Parse A Date-time String In Iso 8601 Format Without Offset-from-utc
Solution 1:
Your date string and DateFormat need to match. For your input "2016-05-21T00:00:00"
, the correct DateFormat is:
DateFormatdf=newSimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Solution 2:
tl;dr
LocalDateTime.parse( "2016-05-21T00:00:00" )
ISO 8601
The input string 2016-05-21T00:00:00
is in standard ISO 8601 format.
java.time
The java.time classes built into Java 8 and later use the ISO 8601 formats by default when parsing or generating textual representations of date-time values.
The input string lacks any offset-from-UTC or time zone info. So the string by itself is imprecise, has no specific meaning as it is not a moment on the timeline. Such values are represented in java.time by the LocalDateTime
class.
Stringinput="2016-05-21T00:00:00";
LocalDateTimeldt= LocalDateTime.parse( input );
If you know from a larger context that the string is intended to have meaning for a certain offset-from-UTC, assign a ZoneOffset
to get a OffsetDateTime
object.
ZoneOffsetzoneOffset= ZoneOffset.of( 5 , 30 );
OffsetDateTimeodt= ldt.atOffset( zoneOffet );
Even better, if you are certain of an intended time zone, specify a ZoneId
to get a ZonedDateTime
.
ZoneIdzoneId= ZoneId.of( "Asia/Kolkata" );
ZonedDateTimezdt= ldt.atZone( ZoneId );
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.
Using a JDBC driver compliant with JDBC 4.2 or later, you may exchange java.time objects directly with your database. No need for strings nor java.sql.* classes.
Where to obtain the java.time classes?
- Java SE 8, Java 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 Java SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
- Later versions of Android bundle implementations of the java.time classes.
- For earlier Android, the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). 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.
Solution 3:
String expectedDateFormat=getDateFromString("2016-05-21 00:00:00","yyyy-MM-dd HH:mm:ss","yyyy MM dd HH:mm:ss");
This method will perform actual format to expected format.
publicstaticStringgetDateFromString(String dateInString, String actualformat, String exceptedFormat) {
SimpleDateFormat form = newSimpleDateFormat(actualformat);
String formatedDate = null;
Date date;
try {
date = form.parse(dateInString);
SimpleDateFormat postFormater = newSimpleDateFormat(exceptedFormat);
formatedDate = postFormater.format(date);
} catch (Exception e) {
e.printStackTrace();
}
return formatedDate;
}
Solution 4:
publicclasstest {
publicstaticvoidmain(String [] args) {
// ref http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.htmlSimpleDateFormat simpleDateFormat = newSimpleDateFormat("yyyy.MM.dd hh:mm:ss");
System.out.println("Current Date: " + simpleDateFormat.format(newDate()));
}
}
for more information on SimpleDateFormat
Solution 5:
try this way
Stringd="2016-05-21 00:00:00";
DateFormatdf=newSimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
DatemyDate=null;
try {
myDate = df.parse(d);
} catch (ParseException e) {
e.printStackTrace();
}
EDIT
Stringd="2016-05-21 00:00:00";
DateFormatdf=newSimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
DatemyDate=null;
try {
myDate = df.parse(d);
} catch (ParseException e) {
e.printStackTrace();
}
Post a Comment for "Parse A Date-time String In Iso 8601 Format Without Offset-from-utc"