Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a date format in epoch

I have a string with a date format such as

Jun 13 2003 23:11:52.454 UTC 

containing millisec... which I want to convert in epoch. Is there an utility in Java I can use to do this conversion?

like image 531
user804979 Avatar asked Jul 14 '11 01:07

user804979


People also ask

What is epoch in date format?

In a computing context, an epoch is the date and time relative to which a computer's clock and timestamp values are determined. The epoch traditionally corresponds to 0 hours, 0 minutes, and 0 seconds (00:00:00) Coordinated Universal Time (UTC) on a specific date, which varies from system to system.

How do I convert time stamps to dates?

The constructor of the Date class receives a long value as an argument. Since the constructor of the Date class requires a long value, we need to convert the Timestamp object into a long value using the getTime() method of the TimeStamp class(present in SQL package).

How do I convert epoch time to manual date?

You can take an epoch time divided by 86400 (seconds in a day) floored and add 719163 (the days up to the year 1970) to pass to it. Awesome, this is as manual as it gets.


1 Answers

This code shows how to use a java.text.SimpleDateFormat to parse a java.util.Date from a String:

String str = "Jun 13 2003 23:11:52.454 UTC"; SimpleDateFormat df = new SimpleDateFormat("MMM dd yyyy HH:mm:ss.SSS zzz"); Date date = df.parse(str); long epoch = date.getTime(); System.out.println(epoch); // 1055545912454 

Date.getTime() returns the epoch time in milliseconds.

like image 168
Bohemian Avatar answered Sep 30 '22 18:09

Bohemian