简体   繁体   中英

Formatting date in java

I am trying to format date string ex. 2014-11-24T18:30:00.000Z to 2014-11-24 using this code below:

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); 
dateFormat.format(reqJsonObj.getString(FROM_DATE));

But it raises exception

java.lang.IllegalArgumentException: Cannot format given Object as a Date

Use dateFormat.parse() instead of dateFormat.format() , since you want to parse your String into Date object. Then, when you have the Date object, format it to String with wanted format. @Jens already gave you the full code, so no need to copy it again here.

You have to parse the string first as a date and then format the date:

SimpleDateFormat dateFormatP = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); 
Date parsedDate = dateFormatP.parse(reqJsonObj.getString(FROM_DATE));
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); 
dateFormat.format(parsedDate );

You need to give a date as the argument for format not a String. First create a SimpleDateFormat to parse the string to a date then give the date object to the other SimpleDateFormat to format the date.

USe this

Date date = formatter.parse(dateInString);

You specified one pattern, appropriate for the generating of a string you seek as your output. But you did define a pattern for parsing the input. You need to go from a string through parsing to a date-time object. Then use that object for generating a string.

Do not think of a string as a date-time but as a textual representation of a date-time value. Think of the date-time object as the actual value.

Avoid old date-time classes

You are using the old date-time classes that are poorly designed, confusing, and troublesome. Avoid classes such as java.util.Date/.Calendar.

java.time

Those old classes are supplanted by the java.time framework built into Java 8 and later.

An Instant is a moment on the timeline in UTC.

Instant now = Instant.now();

ISO 8601

The ISO 8601 standard defined sensible formats for textual representations of date-time values.

Both your input and output strings happen to comply with this standard.

The java.time classes use ISO 8601 as their default for parsing and generating strings. So no need for you to specify formatting patterns.

The Z on the end is short for Zulu which means UTC .

Instant instant = Instant.parse( "2014-11-24T18:30:00.000Z" );

What you want is a date-only value without time-of-day. The LocalDate class serves that purpose.

Determining a date requires an offset-from-UTC (or a full time zone). The Instant class is always in one particular offset, an offset of zero ( UTC ), but is not really aware of that fact. Instant is only a basic building-block class.

The OffsetDateTime class is savvy about various offsets including UTC. We need to specify an explicit offset to make an OffsetDateTime . We will specify the handy constant for UTC, ZoneOffset.UTC .

OffsetDateTime odt = OffsetDateTime.of( instant , ZoneOffset.UTC );

The Instant and the OffsetDateTime are both a moment on the timeline in UTC. The difference is that OffsetDateTime is more flexible and has more features. We can ask the OffsetDateTime for our desired LocalDate object.

LocalDate localDate = odt.toLocalDate();

Now we simply call toString to generate output in standard ISO 8601 format.

String output = localDate.toString();

2014-11-24

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM