CopyPastor

Detecting plagiarism made easy.

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

Possible Plagiarism

Reposted on 2024-12-21
by Arvind Kumar Avinash

Original Post

Original - Posted on 2024-10-17
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 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.
**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. **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)
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<sup>*</sup>.
## Solution using modern date-time API
You can use [`Instant#ofEpochSecond`](https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html#ofEpochSecond-long-) to get an `Instant` from epoch seconds. **Demo:** import java.time.*; public class Main { public static void main(String[] args) { Instant instant = Instant.ofEpochSecond(1442915733L); System.out.println(instant); } }
**Output:**
```none 2015-09-22T09:55:33Z ```
[Online Demo](https://ideone.com/NBma9a)
**Note:** If for some reason, you need an instance of `java.util.Date`, convert `instant` from the above code into a `java.util.Date` instance using `Date date = Date.from(instant)`.
Learn more about the modern Date-Time API from **[Trail: Date Time](https://docs.oracle.com/javase/tutorial/datetime/index.html)**.
**// EDIT**
As suggested by https://stackoverflow.com/users/5772882/anonymous, it's useful to show how to get the current date-time with timezone and how to format it. The below code demonstrates the same: public class Main { public static void main(String[] args) { // ZoneId.systemDefault() returns the ZoneId of the JVM executing the code. // Replace it with the applicable ZoneId e.g. ZoneId.of("America/New_York") ZonedDateTime now = ZonedDateTime.now(ZoneId.systemDefault()); // Default format System.out.println(now); // Custom format DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu.MM.dd HH:mm:ss z", Locale.ENGLISH); System.out.println(now.format(formatter)); } }
**Output:**
```sh 2024-10-17T21:36:48.202859Z[GMT] 2024.10.17 21:36:48 GMT ```
[Online Demo](https://ideone.com/BlsDFk)
--- <sup>* If you are receiving an instance of `java.util.Date`, convert it to`java.time.Instant`, using [`Date#toInstant`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Date.html#toInstant()) and derive other date-time classes of `java.time` from it as per your requirement. </sup>

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