Convert string to Date in java

前端 未结 6 1394
没有蜡笔的小新
没有蜡笔的小新 2020-11-30 02:30

I\'m trying to parse a string to a date field in an android application but I can\'t seem to get it correct. Here is the string I\'m trying to convert to a date \"03/26/2012

相关标签:
6条回答
  • 2020-11-30 02:39

    This code will help you to make a result like FEB 17 20:49 .

        String myTimestamp="2014/02/17 20:49";
    
        SimpleDateFormat form = new SimpleDateFormat("yyyy/MM/dd HH:mm");
        Date date = null;
        Date time = null;
        try 
        {
            date = form.parse(myTimestamp);
            time = new Date(myTimestamp);
            SimpleDateFormat postFormater = new SimpleDateFormat("MMM dd");
            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
            String newDateStr = postFormater.format(date).toUpperCase();
            String newTimeStr = sdf.format(time);
            System.out.println("Date  : "+newDateStr);
            System.out.println("Time  : "+newTimeStr);
        }
        catch (Exception e) 
        {
            e.printStackTrace();
        }
    

    Result :

    Date : FEB 17

    Time : 20:49

    0 讨论(0)
  • 2020-11-30 02:44
    String str_date="13-09-2011";
    DateFormat formatter ; 
    Date date ; 
    formatter = new SimpleDateFormat("dd-MM-yyyy");
    date = (Date)formatter.parse(str_date); 
    System.out.println("Today is " +date.getTime());
    

    Try this

    0 讨论(0)
  • 2020-11-30 02:50
    SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
    String dateInString = "07/06/2013";
    
    try {
    
        Date date = formatter.parse(dateInString);
        System.out.println(date);
        System.out.println(formatter.format(date));
    
    } catch (ParseException e) {
        e.printStackTrace();
    }
    

    Output:

    2014/08/06 16:06:54
    2014/08/06 16:06:54
    
    0 讨论(0)
  • 2020-11-30 02:57

    it went OK when i used Locale.US parametre in SimpleDateFormat

    String dateString = "15 May 2013 17:38:34 +0300";
    System.out.println(dateString);
    
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMM yyyy HH:mm:ss Z", Locale.US);
    DateFormat targetFormat = new SimpleDateFormat("dd MMM yyyy HH:mm", Locale.getDefault());
    String formattedDate = null;
    Date convertedDate = new Date();
    try {
         convertedDate = dateFormat.parse(dateString);
    System.out.println(dateString);
    formattedDate = targetFormat.format(convertedDate);
    } catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
     System.out.println(convertedDate);
    
    0 讨论(0)
  • 2020-11-30 03:03

    You are wrong in the way you display the data I guess, because for me:

        String dateString = "03/26/2012 11:49:00 AM";
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss aa");
        Date convertedDate = new Date();
        try {
            convertedDate = dateFormat.parse(dateString);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println(convertedDate);
    

    Prints:

    Mon Mar 26 11:49:00 EEST 2012
    
    0 讨论(0)
  • 2020-11-30 03:04
    GregorianCalendar date;
    
    CharSequence dateForMart = android.text.format.DateFormat.format("yyyy-MM-dd", date);
    
    Toast.makeText(LogmeanActivity.this,dateForMart,Toast.LENGTH_LONG).show();
    
    0 讨论(0)
提交回复
热议问题