简体   繁体   中英

Java converting string, having millisecond to date object

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!

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.

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

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

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());

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 );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM