-1

In android application I'm trying to parse Date from string to find difference between two times.

SimpleDateFormat dateFormatDateAndTime = new SimpleDateFormat("ddMMyyyyHHMM");
Date startDateTime = dateFormatDateAndTime.parse("060420150134");
Log.v(TAG, "startDateTime: " + startDateTime);  //Fri Oct 06 01:00:00 GMT+05:30 2017   

So, I have to parse 060420150134 so that I get 06 Apr 2015 01:34 and for that my format is ddMMyyyyHHMM

But I get here Fri Oct 06 01:00:00 GMT+05:30 2017

So i'm doing what's wrong? How to parse here?

Shirish Herwade
  • 11,461
  • 20
  • 72
  • 111
  • 2
    You're using `MM` twice. The second one should be `mm`, for minutes rather than months. (I'd also suggest that the bulk of the code in the question is irrelevant; you just need three lines: create the SimpleDateFormat, parse the text, print the result.) – Jon Skeet Apr 08 '15 at 10:40
  • possible duplicate of [parse a date from string gives exception in android](http://stackoverflow.com/questions/29092277/parse-a-date-from-string-gives-exception-in-android) – Rince Thomas Apr 08 '15 at 10:41
  • @JonSkeet so MM is used for month and mm for minutes ? Am I right? – Shirish Herwade Apr 08 '15 at 10:53
  • 1
    JonSkeet is correct....@JonSkeet, You should put it as answer..... – Opiatefuchs Apr 08 '15 at 10:57
  • @Opiatefuchs yes, he just missed some points here – Shirish Herwade Apr 08 '15 at 11:00
  • 1
    @Opiatefuchs No, because this is effectively just a typo question, IMO. I voted to close rather than answering... I'd rather note encourage questions which are just about tiny slips of date formats which are easily fixed by reading the docs. (They're unlikely to help others - as witnessed by the fact that the OP didn't find the various duplicates of this question. It's not like this is the first time that MM and mm have been confused.) – Jon Skeet Apr 08 '15 at 11:04
  • that´s true....that´s really true... :) – Opiatefuchs Apr 08 '15 at 11:07
  • @JonSkeet yes, you are absolutely correct, its just a typo question regarding to you, but for me it's not a copy-paste error, or typing error, I never used these things before, so it's a proper answer to my question which saved my lot of hours, and so you deserve here some points for that – Shirish Herwade Apr 08 '15 at 11:10
  • and now I got my answer, so yes you can now raise as many close flags as you want... – Shirish Herwade Apr 08 '15 at 11:12

3 Answers3

3

You need to change the second MM (months) to mm (minutes):

SimpleDateFormat dateFormatDateAndTime = new SimpleDateFormat("ddMMyyyyHHmm");
FuRioN
  • 623
  • 5
  • 12
0
 Log.v(TAG, "startDateTime: " + startDateTime + "stopDateTime: " + stopDateTime); 
//Fri Oct 06 01:00:00 GMT+05:30 2017   Fri Jan 06 04:00:00 GMT+05:30 2017

You output date object, not string. When you use this: "startDateTime: " + startDateTime + "stopDateTime: " + stopDateTime, java will call method toString() in stopDateTime and startDateTime objects.

RSCh
  • 171
  • 6
0

tl;dr

LocalDateTime.parse( 
    "060420150134" , 
    DateTimeFormatter.ofPattern( "ddMMuuuuHHmm" )
).atOffset( ZoneOffset.UTC )

Using java.time

As the correct accepted Answer says, your formatting pattern is incorrect.

Also, you are using the troublesome old date-time classes that are now legacy, supplanted by the java.time classes. See below for Android info.

The formatting pattern codes are similar but not exactly identical. So study the class doc for DateTimeFormatter.

String input = "060420150134" ;
DateTimeFormatter f = DateTimeFormatter.ofPattern( "ddMMuuuuHHmm" );

Your input string lacks any indicator of offset-from-UTC or time zone. So we parse as a LocalDateTime.

LocalDateTime ldt = LocalDateTime.parse( input , f );

If you know for certain the time zone or offset intended for this data, apply a ZoneId to get a ZonedDateTime or apply a ZoneOffset to get a OffsetDateTime. Apparently in your context UTC itself is intended.

OffsetDateTime odt = ldt.atOffset( ZoneOffset.UTC ) ; 

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154