Sara
Sara

Reputation: 115

Convert dateTime to date in dd/MM/yy format in java

I have a Joda-Time 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.

Upvotes: 5

Views: 60937

Answers (4)

Basil Bourque
Basil Bourque

Reputation: 338690

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.

Upvotes: 2

ravthiru
ravthiru

Reputation: 9633

Using Java 8 provided date-time classes

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

Upvotes: 0

Thomas W
Thomas W

Reputation: 14164

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

Upvotes: 10

kdureidy
kdureidy

Reputation: 960

Here is how you do it

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

Upvotes: 7

Related Questions