2

I have a string "12/9/2010 4:39:38 PM" which i have to convert to a date object. I am using the following code to do it:

String str = "12/9/2010 4:39:38 PM";

DateFormat formatter ;

Date date ;

formatter = new SimpleDateFormat("M/dd/yyyy H:m:s a");

date =(Date)formatter.parse(str);             

System.out.println("date printed"+date);      

However, when im printing the output, i see

Thu Dec 09 04:39:38 IST 2010

How do I get the date exactly the way I declared in the string i.e

12/9/2010 4:39:38 PM

as output? Pls help

Bohemian
  • 412,405
  • 93
  • 575
  • 722
Surya Chandra
  • 1,561
  • 5
  • 27
  • 42
  • Why you need to convert to again same format? – kosa Feb 10 '12 at 16:20
  • `System.out.println("date printed"+date);` is referencing the Date.toString() method. That is why the format of the printed date looks "odd", that is the format used by toString(). If you want to display a date in something other than the default format, you must use a date formatter (as shown in all, current answers below). – DwB Feb 10 '12 at 16:22
  • @thinksteep: I have these dates in string format "12/9/2010 4:39:38 PM" in an arraylist. I have to find the most recent of them. So I need to sort them. For sorting, I cannot perform string sorting. So im converting it into date object. Then sorting is performed with a custom sort and then when im trying to print the latest date, its format is getting changed to "Thu Dec 09 04:39:38 IST 2010". That was the problem i faced. Now its resolved. – Surya Chandra Feb 11 '12 at 07:44
  • [The standard library does not support a formatted Date-Time object.](https://stackoverflow.com/a/68009408/10819573) – Arvind Kumar Avinash Jul 16 '21 at 19:13

8 Answers8

5

You're assuming that the Date value itself remembers the format - it doesn't. Date.toString will do what it wants - because the Date only represents an instant in time.

If you want to format a Date, use your formatter again:

System.out.println(formatter.format(date));

However, that won't necessarily return the exact same value that was in your string, as there may be multiple values which parse the same way. For example, as you've only used "H:m:s", I'd expect "4:5:6" to be parsed the same way as "04:05:06".

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
2

You can entirely specify the format of your date output using the class Formatter

Short answer

String str = "12/9/2010 4:39:38 PM";
Formatter formatter = new SimpleDateFormat("M/dd/yyyy H:m:s a");
Date date =(Date)formatter.parse(str);
Formatter formatterOutput = new SimpleDateFormat("MM/dd/yyyy HH:m:ss a");
String s = formatterOutput.format(date);

Other examples

Format formatter;

// The year
formatter = new SimpleDateFormat("yy");    // 02
formatter = new SimpleDateFormat("yyyy");  // 2002

// The month
formatter = new SimpleDateFormat("M");     // 1
formatter = new SimpleDateFormat("MM");    // 01
formatter = new SimpleDateFormat("MMM");   // Jan
formatter = new SimpleDateFormat("MMMM");  // January

// The day
formatter = new SimpleDateFormat("d");     // 9
formatter = new SimpleDateFormat("dd");    // 09

// The day in week
formatter = new SimpleDateFormat("E");     // Wed
formatter = new SimpleDateFormat("EEEE");  // Wednesday

// Get today's date
Date date = new Date();

// Some examples
formatter = new SimpleDateFormat("MM/dd/yy");
String s = formatter.format(date);
// 01/09/02

formatter = new SimpleDateFormat("dd-MMM-yy");
s = formatter.format(date);
// 29-Jan-02

// Examples with date and time; see also
// Formatting the Time Using a Custom Format
formatter = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss");
s = formatter.format(date);
// 2002.01.29.08.36.33

formatter = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss Z");
s = formatter.format(date);
// Tue, 09 Jan 2002 22:14:02 -0500

from: http://www.exampledepot.com/egs/java.text/formatdate.html

Alberto
  • 2,881
  • 7
  • 35
  • 66
1

Use the same formatter:

System.out.println("date printed "+ formatter.format(date));
Bohemian
  • 412,405
  • 93
  • 575
  • 722
0
public static void main(String args[])
{
        String string="2012-09-13"; 
        Date str=processFileDate(string);
        System.out.println(str);
}
public static Date processFileDate(String str)
{ //returns the date or "null" if doesn't exist

    String[] strformat={
            "EEE,dd MMM yyyy","MMM dd, yyyy, hh.mmaa zzz",
            "EEEE, MMMMM dd  yyyy 'at' hh:mm",
            "EEEE, MMMMM dd, yyyy, hh:mm",
            "EEE MMM dd yyyy, hh:mm ",
            "dd MMMMM yyyy'Last updated at' hh:mm zzz",
            "MMM dd, yyyy 'at' hh:mmaa",
            "MMM dd, yyyy 'at'  hh:mmaa zzz",
            "MMMMM dd, yyyy, hh:mm aa zzz",
            "EEE, MMM dd, yyyy hh:mm ",
            "MMMMM dd, yyyy hh:mm zzz",
            "MMMM dd, yyyy hh:mm aa",
            "MMMM dd, yyyy hh:mmaa",
            "MMMM dd, yyyy hh:mm",
            "dd MMMM yyyy hh:mm:ss",
            "dd MMMM yyyy hh:mm",
            "MMMM dd, yyyy",
            "dd MMMM yyyy ",
            "dd MM yy",
            "yyyy MMMM dd",
            "dd'st' MMMM,yyyy",
            "dd'nd' MMMM,yyyy",
            "dd'rd' MMMM,yyyy",
            "MMMM dd,yyyy",
            "MMM dd yy",
            "mm dd yy",
            "yyyy-MM-dd",
            "yyyy-MM-dd HH:mm:ss",
            "E MMM dd hh:mm:ss Z yyyy",
            "EEE, dd MMM yyyy HH:mm:ss Z"

    };

    String temp="null";
    for(int i=0;i<str.length();i++){
        temp=str.substring(i, str.length());
        for(int l=0;l<strformat.length;){
            Date strp=checkformat(temp,strformat[l]);
            if(strp!=null)
            {
                return strp;
            }
            else l++;
        }
     }
    return null;
}

private static Date checkformat(String str, String sdf) {
     SimpleDateFormat sdformat=new SimpleDateFormat(sdf);

       try{
        Date d=sdformat.parse(str);
        return d;
       }catch(Exception e){}

       return null;
}
Rashmi Rai
  • 23
  • 6
0

Converting it back to String using SimpleDateFormat?

m0skit0
  • 25,268
  • 11
  • 79
  • 127
0
formatter = new SimpleDateFormat("MM/dd/yyyy HH:m:ss a");
String temp =formatter.format(date ); 
kosa
  • 65,990
  • 13
  • 130
  • 167
0

Java.util.Date has no concept of an intrinsic format - You need to use the format(java.util.Date d) method to see a formatted String representation of your Date object.

String str = "12/9/2010 4:39:38 PM";

DateFormat formatter = new SimpleDateFormat("M/dd/yyyy H:m:s a");
Date date =(Date)formatter.parse(str);

System.out.println("date printed"+formatter.format(date));
mcfinnigan
  • 11,442
  • 35
  • 28
0

Not sure what you are trying to accomplish. But you'll have to call SimpleDateFormat.format() to get what you are expecting. Printing the date directly will get only toString() implementation of Date

sethu
  • 8,181
  • 7
  • 39
  • 65