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.
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 ”.
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.
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".
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.
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
Here is how you do it
Format formatter = new SimpleDateFormat("dd/MM/yy");
String string = formatter.format(date);
OffsetDateTime.parse( "2012-12-31T13:32:56.483+13:00" ).toLocalDate()
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
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.
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
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With