简体   繁体   中英

XML date/time formatting in Java

I'm creating a program in Java that alerts me of Earthquakes in Japan, based around an online API that's formatted in XML.

The output date/time from said API seems to be some sort of jumbled mess, and i'm unsure how I could decode this into a readable format.

Would anyone happen to know a good way to decode this into a readable format in Java?

The date is the number of seconds since January 1st, 1970.

To convert it to a date:

  • Multiply by 1000 to convert to milliseconds
  • Create a new Date with it
  • Format the date

Resulting code:

long millis = seconds * 1000;
Date date = new Date(millis);
DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
String formattedDate = df.format(date);

It looks like the eq_date in the XML response is standard epoch so you could simply use that value in a Date object like this

var time = new Date(quake.eq_date);
console.log(time.toString());

You could also use moment.js to help with formatting the output for presentation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM