A Y
A Y

Reputation: 177

Java converting a Date to a different format

I have a date string in this format:

String fieldAsString = "11/26/2011 14:47:31";

I am trying to convert it to a Date type object in this format: "yyyy.MM.dd HH:mm:ss"

I tried using the following code:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
Date newFormat = sdf.parse(fieldAsString);

However, this throws an exception that it is an Unparsable date.

So I tried something like this:

Date date = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss").parse(fieldAsString);
String newFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss").format(date)

However, this new format is now in the 'String' format but I want my function to return the new formatted date as a 'Date' object type. How would I do this?

Thanks!

Upvotes: 0

Views: 1401

Answers (2)

Basil Bourque
Basil Bourque

Reputation: 338594

The answer by Jon Skeet is correct and complete.

Internal to java.util.Date (and Date-Time seen below), the date-time value is stored as milliseconds since the Unix epoch. There is no String inside! When you need a textual representation of the date-time in a format readable by a human, either call toString or use a formatter object to create a String object. Likewise when parsing, the input string is thrown away, not stored inside the Date object (or DateTime object in Joda-Time).

Joda-Time

For fun, here is the (better) way to do this work with Joda-Time, as mentioned by Mr. Skeet.

One major difference is that while a java.util.Date class seems to have a time zone, it does not. A Joda-Time DateTime in contrast does truly know its own time zone.

String input = "11/26/2011 14:47:31";

// From text to date-time.
DateTimeZone timeZone = DateTimeZone.forID( "Pacific/Honolulu" ); // Time zone intended but unrecorded by the input string.
DateTimeFormatter formatterInput = DateTimeFormat.forPattern( "MM/dd/yyyy HH:mm:ss" ).withZone( timeZone );
// No words in the input, so no need for a specific Locale.
DateTime dateTime = formatterInput.parseDateTime( input );

// From date-time to text.
DateTimeFormatter formatterOutput_MontréalEnFrançais = DateTimeFormat.forStyle( "FS" ).withLocale( java.util.Locale.CANADA_FRENCH ).withZone( DateTimeZone.forID( "America/Montreal" ) );
String output = formatterOutput_MontréalEnFrançais.print( dateTime );

Dump to console…

System.out.println( "input: " + input );
System.out.println( "dateTime: " + dateTime );
System.out.println( "dateTime as milliseconds since Unix epoch: " + dateTime.getMillis() );
System.out.println( "dateTime in UTC: " + dateTime.withZone( DateTimeZone.UTC ) );
System.out.println( "output: " + output );

When run…

input: 11/26/2011 14:47:31
dateTime: 2011-11-26T14:47:31.000-10:00
dateTime as milliseconds since Unix epoch: 1322354851000
dateTime in UTC: 2011-11-27T00:47:31.000Z
output: samedi 26 novembre 2011 19:47

Search StackOverflow for "joda" to find many more examples.

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500525

You seem to be under the impression that a Date object has a format. It doesn't. It sounds like you just need this:

Date date = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss").parse(fieldAsString);

(You should consider specifying a locale and possibly a time zone, mind you.)

Then you've got your Date value. A format is only relevant when you later want to convert it to text... that's when you should specify the format. It's important to separate the value being represent (an instant in time, in this case) from a potential textual representation. It's like integers - there's no difference between these two values:

int x = 0x10;
int y = 16;

They're the same value, just represented differently in source code.

Additionally consider using Joda Time for all your date/time work - it's a much cleaner API than java.util.*.

Upvotes: 6

Related Questions