I am unsure of the timestamp format for the value 1522899000000. Can it be used in both Ionic and AngularJS? And how would one go

While parsing a JSON from an external URL, I came across a timestamp with the value starttime: 1522899000000 (on 4/5/2018 -> 5th April 2018). The value seemed like a Unix timestamp, but when I tried to convert it, it displayed the year 1912. What kind of timestamp is this? I am fetching this JSON data from a Java application. How can I convert it to a Java datetime format?

Answer №1

Summary

It appears that the number 1522899000000 represents the count of milliseconds since the epoch reference of 1970-01-01T00:00:00Z, the beginning of 1970 in UTC.

Instant.ofEpochMilli( 1_522_899_000_000L )

This translates to: 2018-04-05T03:30:00Z

Utilizing java.time

The recommended approach is to utilize the java.time classes instead of the outdated Date, Calendar, and SimpleDateFormat classes.

If your input signifies milliseconds since the epoch of 1970-01-01T00:00Z, you can interpret it as an Instant. The Instant class defines a moment on the UTC timeline with nanosecond precision.

long input = 1_522_899_000_000L ;
Instant instant = Instant.ofEpochMilli( input );

The instant is represented as: 2018-04-05T03:30:00Z

If your input is in text form, you can convert it to a number using Long.parse.

long input = Long.parseLong( "1522899000000" ) ;
Instant instant = Instant.ofEpochMilli( input );

ZonedDateTime

An Instant is always in UTC but can be viewed in a specific time zone by creating a ZonedDateTime object.

To obtain the time in a particular region's time zone, specify a valid time zone in the format of continent/region, such as America/Montreal or Pacific/Auckland.

ZoneId z = ZoneId.of( "Pacific/Auckland" ) ;
ZonedDateTime zdt = instant.atZone( z ) ;

The adjusted time in the specified time zone is: 2018-04-05T15:30+12:00[Pacific/Auckland]

DateTimeFormatter

The timestamps generated adhere to the ISO 8601 standard. For alternative formats, refer to Stack Overflow for guidance on using the DateTimeFormatter class.


Introduction to java.time

The java.time framework is integrated into Java versions 8 and above. These classes replace outdated date-time classes like java.util.Date, Calendar, and SimpleDateFormat.

For further information, consult the Oracle Tutorial and explore examples on Stack Overflow. The specification for these classes is outlined in JSR 310.

Direct database interaction using java.time objects is supported. For JDBC drivers compliant with JDBC 4.2 or later, strings or java.sql.* classes are no longer necessary.

Wondering where to find the java.time classes?

  • Java SE 8 and beyond: bundled within the standard API
  • Java SE 6 and 7: ThreeTen-Backport provides back-ported functionality
  • Android: newer Android versions contain implementations, while older versions can utilize ThreeTenABP

The ThreeTen-Extra project extends java.time with additional classes to enhance its capabilities.

Answer №2

Give this timestamp a shot with the 10 digit version

 import java.text.DateFormat;
 import java.text.SimpleDateFormat;
 import java.util.Date;
 public class HelloUniverse{

//1522899000
public static void main(String[] args) throws Exception {

    Date d = new Date((long)1522899000*1000);
    DateFormat f = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.mmm'Z'");
    System.out.println(f.format(d));
 }
}

Author

2018-04-05T03:30:00.030Z

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Receiving array data in a Javascript function and storing it within a variable

Hello everyone, please take a look at my code below. I am attempting to pass PHP array values to a JavaScript function. When I run the script, I receive alerts for parameter0=1, parameter1=2, and parameter2=3 separately. What I am trying to achieve is to ...

Is it possible to consolidate geometry in each frame during the rendering process using three.js?

Having recently delved into three.js, I've been experimenting with some concepts. My current challenge involves creating a line in the scene and using it as a reference for cloning and transforming. The goal is to display the cloned lines in a sequent ...

Is there a way to successfully parse this JSON without encountering any exceptions?

Below is the JSON data that I am working with: [ { "outcome": "Success", "message": "", "identity": "", "delay": "0", "symbol": "AAPL", "companyname": "Apple Inc.", "date": "Jun 08", "time": " 4:52 PM EDT", "open" ...

Acquiring the input from a text box to generate a hyperlink on the current page

I am trying to generate a link that has the href attribute as follows: app\process?val=12000 The catch is that the value for the val parameter (in this case 12000) needs to be derived from a textbox on the page. I am aware that I can retrieve the ...

Disable setTimeout in Node.js triggered by an event

I am facing a dilemma with my code that constantly polls a service and I am looking for a way to efficiently cancel the interval using `clearTimeout` through events. The timeouts essentially act as intervals by calling setTimeout again within the function. ...

Tips for resolving the issue: "Encountered error loading module script..." in Angular 8 and Electron 5

I have been working on developing an Electron 5 app using Angular 8. Despite following numerous online tutorials, I keep encountering the same error. Initially, I set up a new project and ran ng serve --open, which successfully displayed the default angul ...

The JavaScript code is not running as expected!

i have this index.html file: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" ...

The error message "system.js:4 Uncaught (in promise) error" popped up unexpectedly as I was working on my ES

I'm dipping my toes into ES6 development using Plunker. I've loaded Traceur and SystemJS, but I'm encountering the following errors: GET https://traceur-compiler.googlecode.com/git/bin/traceur.js 404 () VM490 system.js:4 GET https://run.pln ...

Performing two consecutive nested AJAX requests in jQuery using JavaScript

I am facing an issue with my code. My goal is to create a cryptocurrency ranking feature on my website. I have integrated an API that provides logos, symbols, indices, prices, etc. of cryptocurrencies. However, I encountered a problem as this API doesn&apo ...

When fetching data from a parse object, JavaScript displayed [object, Object] as the result

When trying to access information from my parse data, I am able to display user table data on my document without any issues. However, when I attempt to query another object and insert it into an id element using jQuery with functions like text();, html(); ...

Tips for preserving newly add row with the help of jquery and php

Currently, I am attempting to implement a functionality on a Wordpress theme options page that dynamically adds rows using jQuery. Below is the HTML code snippet from the THEME-OPTIONS page <a href="#" title="" class="add-author">Add Author</ ...

The art of controlling iframe elements with jquery

I've been researching various topics related to this issue, but I'm still unable to achieve the desired outcome. Currently, I am embedding an iframe within an HTML document like so: <iframe class="full-screen-preview__frame" id="nitseditpre ...

Issue with data table row click event not functioning

I am fetching data from a database using coldfusion to populate a dataTable. I am looking to implement a feature where clicking on a row in the dataTable triggers an event to display that row's details in divs on the same page. The code I have curren ...

Is it possible to configure multiple test suites in a single testng.xml file using Maven, TestNG, and Selenium, and execute them separately at different intervals?

I am currently using Maven, TestNG, and Selenium in my testing framework. At the moment, I have a total of 50 tests in one suite and file. However, I would like to organize these tests into separate suites: Acceptance - This suite will contain five tests ...

avoidable constructor in a react component

When it comes to specifying initial state in a class, I've noticed different approaches being used by people. class App extends React.Component { constructor() { super(); this.state = { user: [] } } render() { return <p>Hi</p> ...

Preventing scrolling in one div while focusing on another div

I am currently in the process of creating a website that functions as a single page site. The main feature of the site is a masonry grid of images. When a user clicks on an item, a detailed panel slides in from the left. Once the panel is closed, it slide ...

Error retrieving data from JSON

I'm attempting to extract a value from the JSON data retrieved via Ajax. Take a look at the example I'm working on here: http://jsfiddle.net/NNrcp/6/. jQuery.ajax({ url:"http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20geo.pla ...

Parent scope receives directive updates with a slight delay

I recently made a transition of my simple paging feature from the controller to a directive, but encountered a peculiar issue. Whenever I update the parent scope from within the directive, the changes only reflect on the next alteration. For instance, if t ...

Exploring the process of retrieving a JSONArray with Retrofit

How can I retrieve the values of districtID(key) and name(key) using retrofit? This is a JSON file... { "error": false, "message": "District successfully fetched.", "districts": [ { "districtID": "DIS260920181", ...

Encoding JSON data with various structures using JSONEncoder

Hey there, I have a collection of JSON Packets as shown below: { "data" : { "lng" : 36.159999999999997, "lat" : 50.359999999999999, "accuracy" : 5 }, "header" : { "type" : "loc" } } and also this one: { "data" : { "time" : ...