How to convert timestamp string to era?

I have a timestamp in format 2017-18-08 11:45:30.345.
I want to transform it into an era, so I do below:

String timeDateStr = "2017-18-08 11:45:30.345"; 
DateTimeFormatter dtf  = DateTimeFormatter.ofPattern("yyyy-dd-MM HH:mm:ss.SSS");
ZonedDateTime     zdt  = ZonedDateTime.parse(timeDateStr, dtf);        
System.out.println(zdt.toInstant().toEpochMilli());

I get below error:

java.time.format.DateTimeParseException: text '2017-18-08 11: 45: 30.345' cannot be parsed: cannot get ZonedDateTime from TemporalAccessor

I also tried different formats, but still got errors.

+4
source share
4 answers

: 2017-18-08 12:60:30.345 ( 60 ), ( 12:60 11:45), (12:60), (11:45).


ZonedDateTime , String ( ).

:

, yyyy-dd-MM HH:mm:ss.SSS, 60 ( , ), ZonedDateTime, /.

, LocalDateTime, , / . UTC:

// change 60 minutes to 59 (otherwise it doesn't work)
String timeDateStr = "2017-18-08 12:59:30.345";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-dd-MM HH:mm:ss.SSS");
// parse to LocalDateTime
LocalDateTime dt = LocalDateTime.parse(timeDateStr, dtf);

// assume the LocalDateTime is in UTC
Instant instant = dt.toInstant(ZoneOffset.UTC);
System.out.println(instant.toEpochMilli());

:

1503061170345

2017-18-08 12:59:30.345 UTC.

, ZoneId:

// get the LocalDateTime in some timezone
ZonedDateTime z = dt.atZone(ZoneId.of("Europe/London"));
System.out.println(z.toInstant().toEpochMilli());

:

1503057570345

, , / Instant ( / 2017-18-08 12:59:30.345 ).

, API IANA ( Region/City, America/Sao_Paulo Europe/Berlin). (, CST PST), .

( , ), ZoneId.getAvailableZoneIds().

ZoneId.systemDefault(), , .


LocalDateTime offset (, -05:00 +03:00):

// get the LocalDateTime in +03:00 offset
System.out.println(dt.toInstant(ZoneOffset.ofHours(3)).toEpochMilli());

/ +03:00 ( 3 UTC):

1503050370345


@MenoHochschild , , 60 ( java.time.format.ResolverStyle):

String timeDateStr = "2017-18-08 12:60:30.345";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-dd-MM HH:mm:ss.SSS")
    // use lenient parsing
    .withResolverStyle(ResolverStyle.LENIENT);
// parse to LocalDateTime
LocalDateTime dt = LocalDateTime.parse(timeDateStr, dtf);

60 , LocalDateTime :

2017-08-18T 13:00: 30,345


UTC ( ZoneOffset), .

( ZoneId), DST ( ) . , , (America/Sao_Paulo).

- DST 15 th 2017: 1 1 . , 00:00 00:59 . , :

ZoneId zone = ZoneId.of("America/Sao_Paulo");

// October 15th 2017 at midnight, DST starts in Sao Paulo
LocalDateTime d = LocalDateTime.of(2017, 10, 15, 0, 0, 0, 0);
ZonedDateTime z = d.atZone(zone);
System.out.println(z);// adjusted to 2017-10-15T01:00-02:00[America/Sao_Paulo]

DST: 18 th 2018 1 , 23 17 th. , 23:00 23:59 ( DST -DST), , :

// February 18th 2018 at midnight, DST ends in Sao Paulo
// local times from 23:00 to 23:59 at 17th exist twice
LocalDateTime d = LocalDateTime.of(2018, 2, 17, 23, 0, 0, 0);
// by default, it gets the offset before DST ends
ZonedDateTime beforeDST = d.atZone(zone);
System.out.println(beforeDST); // before DST end: 2018-02-17T23:00-02:00[America/Sao_Paulo]

// get the offset after DST ends
ZonedDateTime afterDST = beforeDST.withLaterOffsetAtOverlap();
System.out.println(afterDST); // after DST end: 2018-02-17T23:00-03:00[America/Sao_Paulo]

, DST (-02:00 -03:00). epochMilli.

, DST , .

+6

yyyy-dd-MM. 1-59, 60. 60. . DateFormat.

String timeDateStr = "2017-18-08 12:59:30.345";
DateFormat df = new SimpleDateFormat("yyyy-dd-MM hh:mm:ss.SSS", Locale.ENGLISH);
try {
    Date d = df.parse(timeDateStr);
    System.out.println(d.toInstant().toEpochMilli());
} catch (ParseException e) {
    e.printStackTrace();
}
+2

nagendra547

, : -

String timeDateStr = "2017-18-08 12:59:30.345";
DateFormat df = new SimpleDateFormat("yyyy-dd-mm hh:mm:ss.SSS", Locale.ENGLISH);
try {
    Date d = df.parse(timeDateStr);
    System.out.println(d.toInstant().toEpochMilli());
} catch (ParseException e) {
    e.printStackTrace();
}
0

3 .

  • (2017-18-08 12:60:30.345) Formatter. yyyy-MM-dd HH: mm: ss.SSS yyyy-dd-MM hh: mm: ss.SSS
  • (0-59), 60 .
  • , , ZonedDateTime. LocalDateTime , ZoneId.

:

String timeDateStr = "2017-18-08 12:59:30.345"; 
DateTimeFormatter dtf  = DateTimeFormatter.ofPattern("yyyy-dd-MM HH:mm:ss.SSS");
LocalDateTime date = LocalDateTime.parse(timeDateStr, dtf);
ZonedDateTime     zdt  = date.atZone(ZoneId.of("Europe/London"));
System.out.println(zdt.toInstant().toEpochMilli());
0

Source: https://habr.com/ru/post/1683960/


All Articles