CopyPastor

Detecting plagiarism made easy.

Score: 0.8211733227178611; Reported for: String similarity Open both answers

Possible Plagiarism

Reposted on 2024-12-21
by Arvind Kumar Avinash

Original Post

Original - Posted on 2024-12-21
by Arvind Kumar Avinash



            
Present in both answers; Present only in the new answer; Present only in the old answer;

# `java.time`
In March 2014, Java 8 introduced the modern, `java.time` date-time API which supplanted the [error-prone legacy `java.util` date-time API](https://www.oracle.com/technical-resources/articles/java/jf14-date-time.html). Any new code should use the `java.time` API.
## Solution using modern date-time API
You can get the desired result through below steps:
1. Convert epoch seconds to an `Instant` using [`Instant#ofEpochSecond`](https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html#ofEpochSecond-long-). 2. Convert the `Instant` to a `ZonedDateTime` at the desired time zone e.g. `ZoneId.of("America/New_York")`. 3. Format the `ZonedDateTime` using a `DateTimeFormatter` set with the appropriate pattern. **Demo:** class Main { public static void main(String[] args) { // Convert epoch milliseconds to Instant Instant instant = Instant.ofEpochSecond(1386696238L); System.out.println(instant); // ZoneId.systemDefault() returns the ZoneId set to the JVM. Replace it // as desired e.g. to ZoneId.of("America/New_York") ZonedDateTime zdt = instant.atZone(ZoneId.systemDefault()); System.out.println(zdt); // String representation in a custom format String formatted = zdt.format(DateTimeFormatter.ofPattern( "MM/dd/uuuu", Locale.ENGLISH)); System.out.println(formatted); } }
**Output when executed on a JVM set with UTC zone:** ```none 2013-12-10T17:23:58Z 2013-12-10T17:23:58Z[GMT] 12/10/2013 ```
[Online Demo](https://ideone.com/zuxEfV)
Learn more about the modern Date-Time API from **[Trail: Date Time](https://docs.oracle.com/javase/tutorial/datetime/index.html)**.
# `java.time`
In March 2014, Java 8 introduced the modern, `java.time` date-time API which supplanted the [error-prone legacy `java.util` date-time API](https://www.oracle.com/technical-resources/articles/java/jf14-date-time.html). Any new code should use the `java.time` API.
## Solution using modern date-time API
You can get the desired result through below steps:
1. Convert epoch milliseconds to an `Instant` using [`Instant#ofEpochMilli`](https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html#ofEpochMilli-long-). 2. Convert the `Instant` to a `ZonedDateTime` at the desired time zone e.g. `ZoneId.of("America/Jamaica")`. 3. Format the `ZonedDateTime` using a `DateTimeFormatter` set with the appropriate pattern. **Demo:** class Main { public static void main(String[] args) { // Convert epoch milliseconds to Instant Instant instant = Instant.ofEpochMilli(1404327407738L); System.out.println(instant); // Convert Instant to ZonedDateTime at the desired time zone e.g. // ZoneId.of("America/Jamaica") ZoneId zone = ZoneId.of("America/Jamaica"); ZonedDateTime zdt = instant.atZone(zone); System.out.println(zdt); // String representation in a custom format String formatted = zdt.format(DateTimeFormatter.ofPattern( "MM-dd-uuuu 'at' hh:mm:ss a", Locale.ENGLISH)); System.out.println(formatted); } }
**Output:**
```none 2014-07-02T18:56:47.738Z 2014-07-02T13:56:47.738-05:00[America/Jamaica] 07-02-2014 at 01:56:47 PM ```
[Online Demo](https://ideone.com/6eHEgf)
**Note**: Your pattern has the following problems:
1. You have mistakenly used `m` for a month. The symbol `m` is used for a *minute-of-hour*. The symbol for a *month-of-year* is `M`. 2. You have missed the symbol `a` in your pattern. It is used for *am-pm-of-day*.
Learn more about symbols from the [documentation](https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html).
Make sure you use a `Locale` with the `DateTimeFormatter` because you have am/pm marker in the desired output string. Check [this answer](https://stackoverflow.com/a/65544056/10819573) to learn more about it.
Learn more about the modern Date-Time API from **[Trail: Date Time](https://docs.oracle.com/javase/tutorial/datetime/index.html)**.

        
Present in both answers; Present only in the new answer; Present only in the old answer;