SimpleDateFormat parse 2018-11-08_21h34m46sZ
up vote
0
down vote
favorite
Hi want to convert UTC times to local time and i am doing this
public class TimeZoneN
public static void main( String args ) throws ParseException
String timeStr1 = "2018-11-08_21h34m46sZ";
String formatStr1 = "yyyy-MM-dd_HH'h'mm'm'ss's'Z";
SimpleDateFormat formatter = new SimpleDateFormat( formatStr1 );
formatter.setTimeZone( TimeZone.getTimeZone( "UTC" ) );
Date date1 = formatter.parse( timeStr1 );
System.out.println( date1 );
i am getting a parse exception because od the Z, but when i do
String formatStr1 = "yyyy-MM-dd_HH'h'mm'm'ss'sZ'";
It passes, is that valid though ? Woudn't that consider sZ as a single constant and not Z as the timezone token ?
java timezone simpledateformat utc datetime-parsing
|
show 1 more comment
up vote
0
down vote
favorite
Hi want to convert UTC times to local time and i am doing this
public class TimeZoneN
public static void main( String args ) throws ParseException
String timeStr1 = "2018-11-08_21h34m46sZ";
String formatStr1 = "yyyy-MM-dd_HH'h'mm'm'ss's'Z";
SimpleDateFormat formatter = new SimpleDateFormat( formatStr1 );
formatter.setTimeZone( TimeZone.getTimeZone( "UTC" ) );
Date date1 = formatter.parse( timeStr1 );
System.out.println( date1 );
i am getting a parse exception because od the Z, but when i do
String formatStr1 = "yyyy-MM-dd_HH'h'mm'm'ss'sZ'";
It passes, is that valid though ? Woudn't that consider sZ as a single constant and not Z as the timezone token ?
java timezone simpledateformat utc datetime-parsing
1
Yes, that's valid.Z
is a literal.
– ernest_k
Nov 9 at 18:34
In that case what difference would that make if i dont include it at all ? I tried with String formatStr1 = "yyyy-MM-dd_HH'h'mm'm'ss's'"; and its doing exactly the same job.
– user2133558
Nov 9 at 18:39
Z
is not a literal, it’s a UTC offset of zero, also known as “Zulu time zone”. You need to parse as an offset or your formatter will assume the time zone of your JVM or the time zone set on the formatter and give you an incorrect result (if that didn’t happen to be UTC).
– Ole V.V.
Nov 9 at 19:32
I recommend you avoid theSimpleDateFormat
class. It is not only long outdated, it is also notoriously troublesome. Today we have so much better injava.time
, the modern Java date and time API.DateTimeFormatter.ofPattern("yyyy-MM-dd_HH'h'mm'm'ss's'X").parse(timeStr1, Instant::from)
will give you anInstant
of2018-11-08T21:34:46Z
.
– Ole V.V.
Nov 9 at 19:34
It's not a duplicate i am not asking for the same parsing format Ole.
– user2133558
Nov 9 at 21:18
|
show 1 more comment
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Hi want to convert UTC times to local time and i am doing this
public class TimeZoneN
public static void main( String args ) throws ParseException
String timeStr1 = "2018-11-08_21h34m46sZ";
String formatStr1 = "yyyy-MM-dd_HH'h'mm'm'ss's'Z";
SimpleDateFormat formatter = new SimpleDateFormat( formatStr1 );
formatter.setTimeZone( TimeZone.getTimeZone( "UTC" ) );
Date date1 = formatter.parse( timeStr1 );
System.out.println( date1 );
i am getting a parse exception because od the Z, but when i do
String formatStr1 = "yyyy-MM-dd_HH'h'mm'm'ss'sZ'";
It passes, is that valid though ? Woudn't that consider sZ as a single constant and not Z as the timezone token ?
java timezone simpledateformat utc datetime-parsing
Hi want to convert UTC times to local time and i am doing this
public class TimeZoneN
public static void main( String args ) throws ParseException
String timeStr1 = "2018-11-08_21h34m46sZ";
String formatStr1 = "yyyy-MM-dd_HH'h'mm'm'ss's'Z";
SimpleDateFormat formatter = new SimpleDateFormat( formatStr1 );
formatter.setTimeZone( TimeZone.getTimeZone( "UTC" ) );
Date date1 = formatter.parse( timeStr1 );
System.out.println( date1 );
i am getting a parse exception because od the Z, but when i do
String formatStr1 = "yyyy-MM-dd_HH'h'mm'm'ss'sZ'";
It passes, is that valid though ? Woudn't that consider sZ as a single constant and not Z as the timezone token ?
java timezone simpledateformat utc datetime-parsing
java timezone simpledateformat utc datetime-parsing
edited Nov 10 at 4:35
Ole V.V.
25.8k62550
25.8k62550
asked Nov 9 at 18:31
user2133558
1441318
1441318
1
Yes, that's valid.Z
is a literal.
– ernest_k
Nov 9 at 18:34
In that case what difference would that make if i dont include it at all ? I tried with String formatStr1 = "yyyy-MM-dd_HH'h'mm'm'ss's'"; and its doing exactly the same job.
– user2133558
Nov 9 at 18:39
Z
is not a literal, it’s a UTC offset of zero, also known as “Zulu time zone”. You need to parse as an offset or your formatter will assume the time zone of your JVM or the time zone set on the formatter and give you an incorrect result (if that didn’t happen to be UTC).
– Ole V.V.
Nov 9 at 19:32
I recommend you avoid theSimpleDateFormat
class. It is not only long outdated, it is also notoriously troublesome. Today we have so much better injava.time
, the modern Java date and time API.DateTimeFormatter.ofPattern("yyyy-MM-dd_HH'h'mm'm'ss's'X").parse(timeStr1, Instant::from)
will give you anInstant
of2018-11-08T21:34:46Z
.
– Ole V.V.
Nov 9 at 19:34
It's not a duplicate i am not asking for the same parsing format Ole.
– user2133558
Nov 9 at 21:18
|
show 1 more comment
1
Yes, that's valid.Z
is a literal.
– ernest_k
Nov 9 at 18:34
In that case what difference would that make if i dont include it at all ? I tried with String formatStr1 = "yyyy-MM-dd_HH'h'mm'm'ss's'"; and its doing exactly the same job.
– user2133558
Nov 9 at 18:39
Z
is not a literal, it’s a UTC offset of zero, also known as “Zulu time zone”. You need to parse as an offset or your formatter will assume the time zone of your JVM or the time zone set on the formatter and give you an incorrect result (if that didn’t happen to be UTC).
– Ole V.V.
Nov 9 at 19:32
I recommend you avoid theSimpleDateFormat
class. It is not only long outdated, it is also notoriously troublesome. Today we have so much better injava.time
, the modern Java date and time API.DateTimeFormatter.ofPattern("yyyy-MM-dd_HH'h'mm'm'ss's'X").parse(timeStr1, Instant::from)
will give you anInstant
of2018-11-08T21:34:46Z
.
– Ole V.V.
Nov 9 at 19:34
It's not a duplicate i am not asking for the same parsing format Ole.
– user2133558
Nov 9 at 21:18
1
1
Yes, that's valid.
Z
is a literal.– ernest_k
Nov 9 at 18:34
Yes, that's valid.
Z
is a literal.– ernest_k
Nov 9 at 18:34
In that case what difference would that make if i dont include it at all ? I tried with String formatStr1 = "yyyy-MM-dd_HH'h'mm'm'ss's'"; and its doing exactly the same job.
– user2133558
Nov 9 at 18:39
In that case what difference would that make if i dont include it at all ? I tried with String formatStr1 = "yyyy-MM-dd_HH'h'mm'm'ss's'"; and its doing exactly the same job.
– user2133558
Nov 9 at 18:39
Z
is not a literal, it’s a UTC offset of zero, also known as “Zulu time zone”. You need to parse as an offset or your formatter will assume the time zone of your JVM or the time zone set on the formatter and give you an incorrect result (if that didn’t happen to be UTC).– Ole V.V.
Nov 9 at 19:32
Z
is not a literal, it’s a UTC offset of zero, also known as “Zulu time zone”. You need to parse as an offset or your formatter will assume the time zone of your JVM or the time zone set on the formatter and give you an incorrect result (if that didn’t happen to be UTC).– Ole V.V.
Nov 9 at 19:32
I recommend you avoid the
SimpleDateFormat
class. It is not only long outdated, it is also notoriously troublesome. Today we have so much better in java.time
, the modern Java date and time API. DateTimeFormatter.ofPattern("yyyy-MM-dd_HH'h'mm'm'ss's'X").parse(timeStr1, Instant::from)
will give you an Instant
of 2018-11-08T21:34:46Z
.– Ole V.V.
Nov 9 at 19:34
I recommend you avoid the
SimpleDateFormat
class. It is not only long outdated, it is also notoriously troublesome. Today we have so much better in java.time
, the modern Java date and time API. DateTimeFormatter.ofPattern("yyyy-MM-dd_HH'h'mm'm'ss's'X").parse(timeStr1, Instant::from)
will give you an Instant
of 2018-11-08T21:34:46Z
.– Ole V.V.
Nov 9 at 19:34
It's not a duplicate i am not asking for the same parsing format Ole.
– user2133558
Nov 9 at 21:18
It's not a duplicate i am not asking for the same parsing format Ole.
– user2133558
Nov 9 at 21:18
|
show 1 more comment
1 Answer
1
active
oldest
votes
up vote
0
down vote
You are correct that Z
denotes Zulu time, another name for UTC. You may also think of it as offset zero from UTC. So you will want to parse Z
as an offset to ensure that your time is interpreted correctly.
java.time
However, don’t use SimpleDateFormat
. It’s notoriously troublesome and long outdated. Don’t use Date
either, it too is long outdated and it too has design problems.
DateTimeFormatter formatter
= DateTimeFormatter.ofPattern("uuuu-MM-dd_H'h'm'm's's'X");
String timeStr1 = "2018-11-08_21h34m46sZ";
Instant instant1 = formatter.parse(timeStr1, Instant::from);
System.out.println(instant1);
Output:
2018-11-08T21:34:46Z
For the sake of completeness:
want to convert … to local time
Taking America/Toronto as a random example:
ZonedDateTime dateTime = instant1.atZone(ZoneId.of("America/Toronto"));
System.out.println(dateTime);
Output:
2018-11-08T16:34:46-05:00[America/Toronto]
In a comment you asked about Z
:
what difference would that make if i dont include it at all ?
Two things:
- You want to make sure the string includes the required offset and object if not, or you risk that errors pass unnoticed. It’s called input validation.
- As I mentioned you want to parse
Z
as an offset or you cannot extract an unambiguous point in time from the parsed values.
Link: Oracle tutorial: Date Time explaining how to use java.time
.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
You are correct that Z
denotes Zulu time, another name for UTC. You may also think of it as offset zero from UTC. So you will want to parse Z
as an offset to ensure that your time is interpreted correctly.
java.time
However, don’t use SimpleDateFormat
. It’s notoriously troublesome and long outdated. Don’t use Date
either, it too is long outdated and it too has design problems.
DateTimeFormatter formatter
= DateTimeFormatter.ofPattern("uuuu-MM-dd_H'h'm'm's's'X");
String timeStr1 = "2018-11-08_21h34m46sZ";
Instant instant1 = formatter.parse(timeStr1, Instant::from);
System.out.println(instant1);
Output:
2018-11-08T21:34:46Z
For the sake of completeness:
want to convert … to local time
Taking America/Toronto as a random example:
ZonedDateTime dateTime = instant1.atZone(ZoneId.of("America/Toronto"));
System.out.println(dateTime);
Output:
2018-11-08T16:34:46-05:00[America/Toronto]
In a comment you asked about Z
:
what difference would that make if i dont include it at all ?
Two things:
- You want to make sure the string includes the required offset and object if not, or you risk that errors pass unnoticed. It’s called input validation.
- As I mentioned you want to parse
Z
as an offset or you cannot extract an unambiguous point in time from the parsed values.
Link: Oracle tutorial: Date Time explaining how to use java.time
.
add a comment |
up vote
0
down vote
You are correct that Z
denotes Zulu time, another name for UTC. You may also think of it as offset zero from UTC. So you will want to parse Z
as an offset to ensure that your time is interpreted correctly.
java.time
However, don’t use SimpleDateFormat
. It’s notoriously troublesome and long outdated. Don’t use Date
either, it too is long outdated and it too has design problems.
DateTimeFormatter formatter
= DateTimeFormatter.ofPattern("uuuu-MM-dd_H'h'm'm's's'X");
String timeStr1 = "2018-11-08_21h34m46sZ";
Instant instant1 = formatter.parse(timeStr1, Instant::from);
System.out.println(instant1);
Output:
2018-11-08T21:34:46Z
For the sake of completeness:
want to convert … to local time
Taking America/Toronto as a random example:
ZonedDateTime dateTime = instant1.atZone(ZoneId.of("America/Toronto"));
System.out.println(dateTime);
Output:
2018-11-08T16:34:46-05:00[America/Toronto]
In a comment you asked about Z
:
what difference would that make if i dont include it at all ?
Two things:
- You want to make sure the string includes the required offset and object if not, or you risk that errors pass unnoticed. It’s called input validation.
- As I mentioned you want to parse
Z
as an offset or you cannot extract an unambiguous point in time from the parsed values.
Link: Oracle tutorial: Date Time explaining how to use java.time
.
add a comment |
up vote
0
down vote
up vote
0
down vote
You are correct that Z
denotes Zulu time, another name for UTC. You may also think of it as offset zero from UTC. So you will want to parse Z
as an offset to ensure that your time is interpreted correctly.
java.time
However, don’t use SimpleDateFormat
. It’s notoriously troublesome and long outdated. Don’t use Date
either, it too is long outdated and it too has design problems.
DateTimeFormatter formatter
= DateTimeFormatter.ofPattern("uuuu-MM-dd_H'h'm'm's's'X");
String timeStr1 = "2018-11-08_21h34m46sZ";
Instant instant1 = formatter.parse(timeStr1, Instant::from);
System.out.println(instant1);
Output:
2018-11-08T21:34:46Z
For the sake of completeness:
want to convert … to local time
Taking America/Toronto as a random example:
ZonedDateTime dateTime = instant1.atZone(ZoneId.of("America/Toronto"));
System.out.println(dateTime);
Output:
2018-11-08T16:34:46-05:00[America/Toronto]
In a comment you asked about Z
:
what difference would that make if i dont include it at all ?
Two things:
- You want to make sure the string includes the required offset and object if not, or you risk that errors pass unnoticed. It’s called input validation.
- As I mentioned you want to parse
Z
as an offset or you cannot extract an unambiguous point in time from the parsed values.
Link: Oracle tutorial: Date Time explaining how to use java.time
.
You are correct that Z
denotes Zulu time, another name for UTC. You may also think of it as offset zero from UTC. So you will want to parse Z
as an offset to ensure that your time is interpreted correctly.
java.time
However, don’t use SimpleDateFormat
. It’s notoriously troublesome and long outdated. Don’t use Date
either, it too is long outdated and it too has design problems.
DateTimeFormatter formatter
= DateTimeFormatter.ofPattern("uuuu-MM-dd_H'h'm'm's's'X");
String timeStr1 = "2018-11-08_21h34m46sZ";
Instant instant1 = formatter.parse(timeStr1, Instant::from);
System.out.println(instant1);
Output:
2018-11-08T21:34:46Z
For the sake of completeness:
want to convert … to local time
Taking America/Toronto as a random example:
ZonedDateTime dateTime = instant1.atZone(ZoneId.of("America/Toronto"));
System.out.println(dateTime);
Output:
2018-11-08T16:34:46-05:00[America/Toronto]
In a comment you asked about Z
:
what difference would that make if i dont include it at all ?
Two things:
- You want to make sure the string includes the required offset and object if not, or you risk that errors pass unnoticed. It’s called input validation.
- As I mentioned you want to parse
Z
as an offset or you cannot extract an unambiguous point in time from the parsed values.
Link: Oracle tutorial: Date Time explaining how to use java.time
.
edited Nov 10 at 4:39
answered Nov 10 at 4:25
Ole V.V.
25.8k62550
25.8k62550
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53231484%2fsimpledateformat-parse-2018-11-08-21h34m46sz%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Yes, that's valid.
Z
is a literal.– ernest_k
Nov 9 at 18:34
In that case what difference would that make if i dont include it at all ? I tried with String formatStr1 = "yyyy-MM-dd_HH'h'mm'm'ss's'"; and its doing exactly the same job.
– user2133558
Nov 9 at 18:39
Z
is not a literal, it’s a UTC offset of zero, also known as “Zulu time zone”. You need to parse as an offset or your formatter will assume the time zone of your JVM or the time zone set on the formatter and give you an incorrect result (if that didn’t happen to be UTC).– Ole V.V.
Nov 9 at 19:32
I recommend you avoid the
SimpleDateFormat
class. It is not only long outdated, it is also notoriously troublesome. Today we have so much better injava.time
, the modern Java date and time API.DateTimeFormatter.ofPattern("yyyy-MM-dd_HH'h'mm'm'ss's'X").parse(timeStr1, Instant::from)
will give you anInstant
of2018-11-08T21:34:46Z
.– Ole V.V.
Nov 9 at 19:34
It's not a duplicate i am not asking for the same parsing format Ole.
– user2133558
Nov 9 at 21:18