-1

I am rewriting piece of GO code to java and I have doubths about the following snippet.

Go code:

time.Parse("20060102", someString);

Is it analog of ?

ZonedDateTime zdt = ZonedDateTime.parse(credElements[0], DateTimeFormatter.ofPattern("yyyyMMdd")
kostix
  • 51,517
  • 14
  • 93
  • 176
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
  • 1
    Have you tried to run it? – f1sh Jan 31 '23 at 10:06
  • 2
    That is a `LocalDate`, not a `ZonedDateTime`. There is neither a zone nor a time in that string. – Sweeper Jan 31 '23 at 10:06
  • What is the resulting type/value in Go and what do you expect/want it to be in Java? Looks like @Sweeper is right, the result might just be a *date* without *time (of day)*, offset or zone. – deHaar Jan 31 '23 at 10:31

2 Answers2

4

A quick look at the Go documentation reveals that:

A Time represents an instant in time with nanosecond precision.

Which is similar to Java's Instant type.

Also, from the docs of Parse,

Elements omitted from the layout are assumed to be zero or, when zero is impossible, one, so parsing "3:04pm" returns the time corresponding to Jan 1, year 0, 15:04:00 UTC (note that because the year is 0, this time is before the zero Time).

[...]

In the absence of a time zone indicator, Parse returns a time in UTC.

Knowing this, we can first create a LocalDate from your string that does not contain any zone or time information, then "assume" (as the Go documentation calls it) that it is at the start of day, and at the UTC zone, in order to convert it to an Instant:

var date = LocalDate.parse(someString, DateTimeFormatter.BASIC_ISO_DATE);
var instant = date.atStartOfDay(ZoneOffset.UTC).toInstant();
Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • 1
    To avoid problems, you should use `date.atStartOfDay(ZoneOffset.UTC).toInstant(ZoneOffset.UTC)`. – Arvind Kumar Avinash Jan 31 '23 at 11:21
  • @ArvindKumarAvinash What problems could there be? The start of day is calculated with `LocalDate`, which is very “well behaved”, and not using any time zones, where the start of day could be non-00:00:00. – Sweeper Jan 31 '23 at 11:59
  • This is a great answer. The only thing I was concerned about was that `LocalDate#atStartOfDay()` uses the system's time and theoretically, it may not be always `00:00`. Thanks for updating it in your answer. – Arvind Kumar Avinash Jan 31 '23 at 13:18
  • @OleV.V. Thanks. I usually only use those built-in ones for formatting, because I never made an effort to remember what the exact formats of those look like. Totally didn't know there was a built-in one for this format. – Sweeper Feb 01 '23 at 01:33
2

Since the result of the line of Go you provided includes an offset, a zone and the time of day, you will have to explicitly attach those and use a specific formatter in Java:

public static void main(String[] args) {
    // example input
    String date = "20060102";
    // parse the date first, using a built-in formatter
    LocalDate localDate = LocalDate.parse(date, DateTimeFormatter.BASIC_ISO_DATE);
    // then add the minimum time of day and the desired zone id
    ZonedDateTime zdt = ZonedDateTime.of(localDate, LocalTime.MIN, ZoneId.of("UTC"));
    // the formatter
    DateTimeFormatter dtfOut = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss Z VV", Locale.ENGLISH);
    // the result of the Go statement
    String expected = "2006-01-02 00:00:00 +0000 UTC";
    // print the result
    System.out.println("Expected: " + expected);
    System.out.println("Actual:   " + zdt.format(dtfOut));
}

Output:

Expected: 2006-01-02 00:00:00 +0000 UTC
Actual:   2006-01-02 00:00:00 +0000 UTC

Posts about y and u (actually accepted answers to the questions)

deHaar
  • 17,687
  • 10
  • 38
  • 51
  • I have couple of questions. 1. Why do you use "uuuu-MM-dd HH:mm:ss Z VV" ? 2. String pattern = "uuuuMMdd"; is it typo ? I am about uuuu – gstackoverflow Jan 31 '23 at 11:19
  • @gstackoverflow - `u` is correct. Check https://stackoverflow.com/a/65928023/10819573 to learn more about it. Probably he has used `uuuu-MM-dd HH:mm:ss Z VV` to show you formatted output. – Arvind Kumar Avinash Jan 31 '23 at 11:22
  • @ArvindKumarAvinash though the golang `Time` type doesn’t support negative years :) – Sweeper Jan 31 '23 at 12:07
  • @gstackoverflow A `DateTimeFormatter` can be used for parsing `String`s as well as formatting date-time types as `String`. The longer pattern is the one responsible for the output, the shorter one parses your date `String`. [`u` is year, `y` is year of era](https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html). Depending on your needs, `y` may be more appropriate to use, that's a thing you have to find out yourself. – deHaar Jan 31 '23 at 12:22
  • @Sweeper - "... golang Time type doesn’t support negative years ..." - Note that the OP is migrating his code to Java and that's why `u` becomes so important in this code. His existing data may not have a negative year but his Java application may get date-time with a negative year. – Arvind Kumar Avinash Jan 31 '23 at 13:13