Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 630
Sara Avatar asked Sep 21 '13 01:09

Sara


People also ask

How do you format a date in Java like in the Ddmmyyyy format?

SimpleDateFormat can be created using the SimpleDateFormat constructor. The constructor is a parametrised constructor and needs a String pattern as the parameter. The String pattern is the pattern which will be used to format a date and the output will be generated in that pattern as “ MM-dd-yyyy ”.

How convert date format from DD MM YYYY to Yyyymmdd in Java?

First you have to parse the string representation of your date-time into a Date object. DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = (Date)formatter. parse("2011-11-29 12:34:25"); Then you format the Date object back into a String in your preferred format.

How do I change date format in SimpleDateFormat?

Create Simple Date Format // Date Format In Java String pattern = "yyyy-MM-dd"; SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern); According to the example above, the String pattern will be used to format dates, and the output will be shown as "yyyy-MM-dd".

How do I change date format to date?

On the Home tab, click the popup window launcher next to Number. In the Category box, click Date, and then click the date format that you want in the Type list.


3 Answers

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:

  • http://joda-time.sourceforge.net/apidocs/org/joda/time/DateTime.html#toDateMidnight()
  • http://joda-time.sourceforge.net/apidocs/org/joda/time/base/AbstractInstant.html#toDate()

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
like image 58
Thomas W Avatar answered Oct 15 '22 10:10

Thomas W


Here is how you do it

Format formatter = new SimpleDateFormat("dd/MM/yy");
String string = formatter.format(date);
like image 7
kdureidy Avatar answered Oct 15 '22 10:10

kdureidy


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?

  • Java SE 8 and SE 9 and later
    • Built-in.
    • Part of the standard Java API with a bundled implementation.
    • Java 9 adds some minor features and fixes.
  • Java SE 6 and SE 7
    • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android
    • The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
    • See How to use ThreeTenABP….

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.

like image 2
Basil Bourque Avatar answered Oct 15 '22 08:10

Basil Bourque