0

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());
Cœur
  • 37,241
  • 25
  • 195
  • 267
Darko Petkovski
  • 3,892
  • 13
  • 53
  • 117

3 Answers3

2

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

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

Swayam
  • 16,294
  • 14
  • 64
  • 102
1
 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());
Sireesh Vattikuti
  • 1,180
  • 1
  • 9
  • 21
0

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());
Alan Barrows
  • 579
  • 3
  • 10