Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert date/time string into minutes since Unix epoch?

Tags:

java

date

I need to convert date/time in a text file into number of minutes elapsed since unix epoch (i.e., January 1st, 1970):

e.g. 2006-01-01 07:14:38.000 into 18934874

I'm using Java to parse the file. thanks

like image 416
aneuryzm Avatar asked Feb 20 '11 17:02

aneuryzm


1 Answers

you can use the class SimpleDateFormat to parse the time. For example

SimpleDateFormat sdf  = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss.SSS");
Date date = sdf.parse("2006-01-01 07:14:38.000");
long timeInMillisSinceEpoch = date.getTime(); 
long timeInMinutesSinceEpoch = timeInMillisSinceEpoch / TimeUnit.MILLISECONDS.toMinutes(timeInMillisSinceEpoch);

disclaimer

1) A few minutes ago I realized that I've used the wrong pattern for milliseconds (used 's' instead of 'S'). Sorry for the mistake. 2) Added suggestion from @superfav

like image 181
Augusto Avatar answered Sep 21 '22 17:09

Augusto