aneuryzm
aneuryzm

Reputation: 64834

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

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

Upvotes: 9

Views: 28937

Answers (1)

Augusto
Augusto

Reputation: 29927

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 = 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

Upvotes: 28

Related Questions