5

I have a JODA DateTime 2012-12-31T13:32:56.483+13:00. I would like to convert it to Date in dd/MM/yy format. So I'm expecting code to return Date like - 31/12/12.

Code -

    // Input dateTime = 2012-12-31T13:32:56.483+13:00
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yy");
    Date date = simpleDateFormat.parse(dateTime.toString("dd/MM/yy"));

Results:

Output - Mon Dec 31 00:00:00 NZDT 2012
Expected Output - 31/12/12

When I do the following, I get the expected output but I don't know how to convert it to Date-

String string = simpleDateFormat.format(date); 

Please help me.

Thx

EDIT - I want my end result to be Util Date in dd/MM/yy format. I Do not want String output. My input is Joda DateTime yyyy-MM-ddThh:mm:ss:+GMT. I need to convert JodaDateTime to UtilDate.

5
  • Ya. I don't like it too but db allows only 2 digit year in my schema. Also, I get the same error for 4 digit year.
    – Sara
    Sep 21, 2013 at 2:15
  • 2
    Date doesn't inherently have a format. Date can be formatted to/ or parsed from a String via a specified DateFormat, but it doesn't inherently hold or have a format.
    – Thomas W
    Sep 21, 2013 at 2:25
  • I am trying to convert JodaTime -> java.util.Date and truncate the time component and get the resulting date in dd/MM/yy format. Thanks
    – Sara
    Sep 21, 2013 at 2:40
  • Great! I'll edit my answer below. Thanks Sara.
    – Thomas W
    Sep 21, 2013 at 2:56
  • Probably this is what you are looking for? stackoverflow.com/questions/5050170/… Sep 21, 2013 at 11:37

4 Answers 4

10

As I said originally, Date objects do not have an inherent format. java.util.Date holds a millisecond time value, representing both date & time. Dates are parsed from string, or formatted to string, via your choice of DateFormat.

The strings may be formatted per specification, but the Date objects behind them are always full precision & do not have any inherent notion of format.


To truncate a combined "date and time" java.util.Date to just the date component, leaving it effectively at midnight:

public static Date truncateTime (Date date) {
    Calendar cal = Calendar.getInstance();
    cal.setTime( date);
    cal.set( Calendar.HOUR_OF_DAY, 0);
    cal.set( Calendar.MINUTE, 0);
    cal.set( Calendar.SECOND, 0);
    cal.set( Calendar.MILLISECOND, 0);
    return cal.getTime();
}

If you're coming from JodaTime DateTime, you can do this more easily working mostly in the JodaTime API.

public static Date truncateJodaDT (DateTime dt) {
    java.util.Date result = dt.toDateMidnight().toDate();
    return result;
}

Hope this helps!

See:


Now I'm unsure again, what you want. You want the date in string format now?

return simpleDateFormat.format( date);    // from java.util.Date

Or with JodaTime:

return dateTime.toString( "dd/MM/yy");    // from org.joda.time.DateTime
8
  • My date variable gives Mon Dec 31 00:00:00 NZDT 2012 as output. When I do format, I get the right format but how do I convert to Date?
    – Sara
    Sep 21, 2013 at 2:13
  • date is a Date. Formatting converts it to String. So you already have the Date parsed correctly! If you really want, you can parse the String back to a Date.. but you already did that once :)
    – Thomas W
    Sep 21, 2013 at 2:22
  • I understand that using format converts date to String. Before that when I do date = simpleDateFormat.parse(dateTime.toLocalDate().toString("dd/MM/yy")); I get Mon Dec 31 00:00:00 NZDT 2012 but I expect it to give me 31/12/12... :(
    – Sara
    Sep 21, 2013 at 2:36
  • 1
    Your question's very unclear. I've edited my post to include the other thing you may be attempting to do, but perhaps you should tell us properly what you're trying to achieve. What datatype of result do you want & how/where do you intend to use that?
    – Thomas W
    Sep 21, 2013 at 2:43
  • 1
    @Sara: As Thomas has said in the comments before, Date objects do not have a format by themselves. So, it's impossible to "have a Date object in the format dd/MM/yy". You specify the format when you convert the Date to a String for formatting. The SimpleDateFormat object knows the format, but not the Date object itself.
    – Jesper
    Sep 21, 2013 at 11:27
7

Here is how you do it

Format formatter = new SimpleDateFormat("dd/MM/yy");
String string = formatter.format(date);
2

tl;dr

OffsetDateTime.parse( "2012-12-31T13:32:56.483+13:00" ).toLocalDate()

Joda-Time supplanted by java.time

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

ISO 8601

Your input string is in standard ISO 8601 format.

The java.time classes use the standard formats by default, so no need to specify a formatting pattern.

Date-time objects vs strings

As the accepted Answer explains, you should not conflate date-time objects with strings that may textually represent their value. A java.util.Date object has no format as it has no text. Ditto for the java.time classes. You can use a java.time object to parse a string, or generate a string, but the object is distinct and separate from the string.

OffsetDateTime

Parse your input string as a OffsetDateTime as it includes an offset-from-UTC though it lacks a time zone.

OffsetDateTime odt = OffsetDateTime.parse( "2012-12-31T13:32:56.483+13:00" );

LocalDate

If you want a date-only value, use the LocalDate class. Remember that for any given moment, the date varies around the globe by time zone. A minute after midnight is a new day in Paris while still “yesterday” in Montréal Québec. If you want the date from that same offset of +13:00, simply call toLocalDate.

LocalDate localDate = odt.toLocalDate();

The Question says you do not want a string, only a date object. So there you go. If you later want a string, call toString for a String to be generated in standard ISO 8601 format, 2012-12-31. For other formats, search Stack Overflow for DateTimeFormatter.


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

0

Using Java 8 provided date-time classes

LocalDate parsedDate = LocalDate.parse(dateStr,DateTimeFormatter.ISO_ZONED_DATE_TIME);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.