1

I have 2 Strings

  • 2012-06-25 15:02:22.948
  • +0530

I need a new string which adds the 5:30 to the time in the first string.

I thought I can do this by converting both strings to date objects and then adding. But i dont know how to do it, as when i use

yyyy MM dd hh:mm:ss as the date format for the first string, I get an error.

Thanks!

Zer0
  • 2,171
  • 3
  • 17
  • 18
  • Your answer is here: [How to calculate time difference in Java](http://stackoverflow.com/questions/4927856/how-to-calculate-time-difference-in-java) – paulsm4 Jul 04 '12 at 07:48
  • But i am not able to parse my string properly to a date object. I think its because of the 22.948, the decimal point – Zer0 Jul 04 '12 at 07:50
  • they are milliseconds...you need to use yyyy MM dd hh:mm:ss.SSS as format – Abhishek bhutra Jul 04 '12 at 07:54
  • The previous comment is a good way to start. Also you get an error because your first string is in "yyyy-MM-dd hh:mm:ss.SSS" format, not the one you provided. – hellodanylo Jul 04 '12 at 07:54
  • Also your second string seems to represent timezone...this specifies IST timezone..I hope that clears you a way to get desired output – Abhishek bhutra Jul 04 '12 at 07:55

5 Answers5

3

The format of the string 2012-06-25 15:02:22.948 is not yyyy MM dd hh:mm:ss, so it's not surprising that you get "an error" (what error is it? the more specific you are, the better people can help you!).

Try yyyy-MM-dd HH:mm:ss.SSS. See the API documentation of SimpleDateFormat to understand the exact syntax of the format string.

Note: Upper and lower case is important in the format string. hh means 12-hour clock, HH means 24-hour clock. If you use hh, parsing 15 for the hours won't work. You also didn't include the milliseconds SSS in the format string.

Jesper
  • 202,709
  • 46
  • 318
  • 350
2

You can merge both you string String1+string2 and can use format yyyy-MM-dd HH:mm:ss.SSSZ to parse the date. You can see more documentation here

Abhishek bhutra
  • 1,400
  • 1
  • 11
  • 29
0

You're getting an exception because the your date format String is wrong. You're giving a date string on the form

"yyyy-MM-dd hh:mm:ss.S"

See SimpleDateFormat javadoc

Gustav Barkefors
  • 5,016
  • 26
  • 30
0

Try this:

    DateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
    Date date = format.parse("2012-06-25 15:02:22.948");
    Calendar calendar = new GregorianCalendar();
    calendar.setTimeInMillis(date.getTime());

    int time = Integer.parseInt("0530");
    int hour = time / 100;
    int minute = time % 100;

    calendar.add(Calendar.HOUR_OF_DAY, hour);
    calendar.add(Calendar.MINUTE, minute);

    String newDateInString = format.format(calendar.getTime());
Meisam
  • 435
  • 1
  • 4
  • 12
0

The other answers are correct but outdated.

java.time

The old date-time classes (java.util.Date/.Calendar etc.) bundled with the earliest versions of Java are now legacy.

Those old classes have been supplanted by the java.time package. See Oracle Tutorial. Much of the functionality has been back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP.

LocalDateTime

The LocalDateTime class represent a date-time without time zone. Use those for the first piece.

Your format is close to standard ISO 8601 format, just replace the SPACE with a T.

String input = "2012-06-25 15:02:22.948";
String inputStandardized = input.replace( " " , "T" );
LocalDateTime ldt = LocalDateTime.parse( inputStandardized );

Offset from UTC

The other piece is the offset-from-UTC. We use the ZoneOffset class for this.

ZoneOffset offset = ZoneOffset.of( "+0530" );

Without an offset or time zone the LocalDateTime is not an actual moment on the timeline but rather a rough idea about a possible moment. Now we add your offset-from-UTC to mark an actual moment, represented by the OffsetDateTime class.

OffsetDateTime odt = OffsetDateTime.of( ldt , offset );

Zoned

A time zone is an offset plus rules for handling anomalies such as Daylight Saving Time (DST). So better to use a time zone than a mere offset.

For example, if the context of this data is known to be time in India, use a time zone such as Asia/Kolkata to get a ZonedDateTime.

ZoneId zoneId = ZoneId.of( "Asia/Kolkata" );
ZonedDateTime zdt = odt.atZoneSameInstant( zoneId );
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154