简体   繁体   English

Java转换字符串,具有毫秒日期对象

[英]Java converting string, having millisecond to date object

I have 2 Strings 我有2个琴弦

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

I need a new string which adds the 5:30 to the time in the first string. 我需要一个新字符串,该字符串将5:30添加到第一个字符串中的时间。

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. yyyy MM dd hh:mm:ss作为第一个字符串的日期格式,出现错误。

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!). 字符串2012-06-25 15:02:22.948的格式不是yyyy MM dd hh:mm:ss ,因此出现“错误”也就不足为奇了(它是什么错误?您越具体,更好的人可以帮助您!)。

Try yyyy-MM-dd HH:mm:ss.SSS . 尝试yyyy-MM-dd HH:mm:ss.SSS See the API documentation of SimpleDateFormat to understand the exact syntax of the format string. 请参阅SimpleDateFormatAPI文档以了解格式字符串的确切语法。

Note: Upper and lower case is important in the format string. 注意:在格式字符串中,大写和小写都很重要。 hh means 12-hour clock, HH means 24-hour clock. hh表示12小时制, HH表示24小时制。 If you use hh , parsing 15 for the hours won't work. 如果您使用hh ,则无法解析15小时。 You also didn't include the milliseconds SSS in the format string. 您还没有在格式字符串中包括毫秒SSS

You can merge both you string String1+string2 and can use format yyyy-MM-dd HH:mm:ss.SSSZ to parse the date. 您可以合并字符串String1 + string2,也可以使用格式yyyy-MM-dd HH:mm:ss.SSSZ来解析日期。 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 请参见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 java.time

The old date-time classes (java.util.Date/.Calendar etc.) bundled with the earliest versions of Java are now legacy. 现在,与最早的Java版本捆绑在一起的旧的日期时间类(java.util.Date/.Calendar等)已成为历史。

Those old classes have been supplanted by the java.time package. 那些旧类已被java.time包所取代。 See Oracle Tutorial . 请参阅Oracle教程 Much of the functionality has been back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP . 许多功能已被后移植到Java 6和7在ThreeTen-反向移植并且还适于在到Android ThreeTenABP

LocalDateTime

The LocalDateTime class represent a date-time without time zone. LocalDateTime类表示没有时区的日期时间。 Use those for the first piece. 将这些用于第一部分。

Your format is close to standard ISO 8601 format, just replace the SPACE with a T . 您的格式接近于标准ISO 8601格式,只需将SPACE替换为T

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

Offset from UTC 与UTC的抵销

The other piece is the offset-from-UTC . 另一个是从UTC偏移 We use the ZoneOffset class for this. 我们为此使用ZoneOffset类。

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. 没有偏移量或时区, LocalDateTime 并不是时间轴上的实际时刻,而是关于可能时刻的粗略想法。 Now we add your offset-from-UTC to mark an actual moment, represented by the OffsetDateTime class. 现在,我们添加您的UTC偏移量以标记实际时刻,由OffsetDateTime类表示。

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

Zoned 划区

A time zone is an offset plus rules for handling anomalies such as Daylight Saving Time (DST). 时区是偏移量以及用于处理异常的规则,例如夏令时(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 . 例如,如果已知此数据的上下文是印度的时间,则使用诸如Asia/Kolkata类的时区来获取ZonedDateTime

ZoneId zoneId = ZoneId.of( "Asia/Kolkata" );
ZonedDateTime zdt = odt.atZoneSameInstant( zoneId );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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