Currently I have a String
field that stores date in a following format:
"2017-04-19 godz. 20:00"
I need to parse it to the format:
2017-04-19T20:00:00Z
Can you give me a hint how can I do it in java
?
Currently I have a String
field that stores date in a following format:
"2017-04-19 godz. 20:00"
I need to parse it to the format:
2017-04-19T20:00:00Z
Can you give me a hint how can I do it in java
?
Use java.time.format.DateTimeFormatter
for Java 8.
Or java.text.SimpleDateFormat
for Java 7.
If anybody else needs sample code:
SimpleDateFormat sourceFormat = new SimpleDateFormat("yyyy-MM-dd 'godz.' HH:mm");
SimpleDateFormat targetFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
String dateStr = "2017-04-19 godz. 20:00";
Date date = sourceFormat.parse(dateStr);
String formattedDate = targetFormat.format(date);
System.out.println(formattedDate);
Output is:
2017-04-19T20:00:00Z
Define a formatting pattern that expects this characters to be present, and ignore them.
Specify a Locale
to determine (a) the human language for translation of name of day, name of month, and such, and (b) the cultural norms deciding issues of abbreviation, capitalization, punctuation, and such. A locale may not have an effect in this particular case, but generally best as a habit to specify a Locale
rather than rely implicitly on the JVM’s current default locale.
Locale locale = new Locale( "pl" , "PL" );
DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuu-MM-dd 'godz.' HH:mm" ).withLocale( locale ) ;
Use that formatter to parse the string as a LocalDateTime
because it lacks any indication of time zone or offset-from-UTC.
String input = "2017-04-19 godz. 20:00" ;
LocalDateTime ldt = LocalDateTime.parse( input , f );
You say you want a value in UTC, but your input string does not indicate any time zone or offset. If you know the offset or zone intended for that string by the context of your business problem, apply the zone or offset. If you do not know the offset/zone, then there is no solution.
I will arbitrarily use the time zone of Europe/Warsaw
as an example.
ZoneId z = ZoneId.of( "Europe/Warsaw" );
ZonedDateTime zdt = ldt.atZone( z );
For UTC, extract an Instant
. The Instant
class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction).
Instant instant = zdt.toInstant();
If the intended zone/offset is UTC, then use an OffsetDateTime
.
OffsetDateTime odt = instant.atOffset( ZoneOffset.UTC );
Your desired output happens to comply with the modern ISO 8601 standard for date-time formats. And the java.time classes use ISO 8601 formats by default when generating/parsing strings. So merely call toString
.
String output = instant.toString();
See this code run live in IdeOne.com.
ldt.toString(): 2017-04-19T20:00
zdt.toString(): 2017-04-19T20:00+02:00[Europe/Warsaw]
instant.toString(): 2017-04-19T18:00:00Z
Be clear that date-time objects are not strings. Date-time objects can parse strings representing date-time values, and can generate strings representing date-time values. But the string objects are distinct and separate from the date-time objects. In other words, date-time objects do not themselves "have a format".
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.