Darko Petkovski
Darko Petkovski

Reputation: 3912

Parse date from string to get milliseconds

I'm trying to get the milliseconds from an string that contains a date, but seems that I'm getting the wrong value. This is the string im trying to parse: 2015-03-01 00:00:00, I'm doing this to parse it:

 DateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
 Date inputDate = dateFormat.parse(data.get(position).getValidTo());
 Log.d("--", inputDate.getDay() + " | " + inputDate.getMonth());

Upvotes: 0

Views: 144

Answers (3)

Swayam
Swayam

Reputation: 16354

Use "MM" instead of "mm" to get month. ("mm" stands for minutes)

And inputDate.getTime() will give the time in milliseconds.

Upvotes: 2

Alan Barrows
Alan Barrows

Reputation: 609

If you add dependencies I would use JodaTime to do anything date related.

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

and use this

String input = "2015-03-01 00:00:00";
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
DateTime dt = DateTime.parse(input, formatter);

System.out.println(dt.getDayOfWeek());
System.out.println(dt.getMonthOfYear());

Upvotes: 0

Sireesh Vattikuti
Sireesh Vattikuti

Reputation: 1190

 DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
 Date inputDate = dateFormat.parse("2014-10-12 12:00:00");
 System.out.println(inputDate.getTime());

Upvotes: 1

Related Questions