java 以日期时间格式添加小时java

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13853910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 14:11:48  来源:igfitidea点击:

adding hours in date time format java

java

提问by user1521582

SimpleDateFormat parser = new SimpleDateFormat("HH:mm");
Date time1 = parser.parse("7:30");

Now if I want to add 2 more hours to time1, like:

现在,如果我想再增加 2 小时time1,例如:

7:30 + 2 = 9:30

how do I add the 2 hours?

我如何添加 2 小时?

回答by Abubakkar

java.util.Dateis deprecated, you should use java.util.Calendarinstead.

java.util.Date已弃用,您应该java.util.Calendar改用。

SimpleDateFormat parser = new SimpleDateFormat("HH:mm");
Date myDate = parser.parse("7:30");
Calendar cal =Calendar.getInstance();
cal.setTime(myDate);
cal.add(Calendar.HOUR_OF_DAY,2); // this will add two hours
myDate = cal.getTime();

And even better solution is to use Joda Time - Java date and time API.

更好的解决方案是使用Joda Time-Java 日期和时间 API

From their website - Joda-Time provides a quality replacement for the Java date and time classes.

从他们的网站 - Joda-Time 提供了 Java 日期和时间类的质量替代品。

回答by PermGenError

Convert java.util.Dateinto java.util.CalendarObject and use Calendar.add()method to add Hours

转换java.util.Datejava.util.CalendarObject 并使用Calendar.add()方法添加小时

SimpleDateFormat parser = new SimpleDateFormat("HH:mm");
Date time1 = parser.parse("7:30");
Calendar cal =Calendar.getInstance();
cal.setTime(time1);
cal.add(Calendar.Hour_Of_Day, 2);
time1 =cal.getTime();
System.out.println(parser.format(time1));//returns 09:30

回答by Basil Bourque

tl;dr

tl;博士

LocalTime.parse( "07:30" ).plusHours( 2 )

…or…

…或者…

ZonedDateTime.now( ZoneId.of( " Pacific/Auckland" ) )
    .plusHours( 2 )

java.time

时间

The old date-time classes such as java.util.Date, .Calendar, and java.text.SimpleDateFormat should be avoided, now supplanted by the java.time classes.

应避免使用旧的日期时间类,例如 java.util.Date、.Calendar 和 java.text.SimpleDateFormat,现在已被 java.time 类取代。

LocalTime

LocalTime

For a time-of-day only value, use the LocalTimeclass.

对于仅限时间的值,请使用LocalTime该类。

LocalTime lt = LocalTime.parse( "07:30" );
LocalTime ltLater = lt.plusHours( 2 );  
String output = ltLater.toString();  // 09:30

Instant

Instant

For a given java.util.Date, convert to java.time using new methods added to the old classes. The Instantclass represents a moment on the timeline in UTC with a resolution of nanoseconds.

对于给定的java.util.Date,使用添加到旧类的新方法转换为 java.time。本Instant类表示UTC时间线与纳秒的分辨率上一会儿。

Instant instant = myUtilDate.toInstant();

Or capture current moment in UTC as an Instant.

或者在 UTC 中捕获当前时刻作为Instant.

Instant instant = Instant.now();

Add two hours as seconds. The TimeUnitclass can convert hours to seconds.

添加两个小时作为秒。该TimeUnit班可以在几个小时转换为秒。

long seconds = TimeUnit.HOURS.toSeconds( 2 );
Instant instantLater = instant.plusSeconds( seconds );

ZonedDateTime

ZonedDateTime

To view in the wall-clock time of some community, apply a time zone. Apply a ZoneIdto get a ZonedDateTimeobject.

要查看某个社区的挂钟时间,请应用时区。应用 aZoneId来获取一个ZonedDateTime对象。

ZoneId z = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = instant.atZone( z );

You can add hours. The ZonedDateTimeclass handles anomalies such as Daylight Saving Time.

您可以添加小时数。ZonedDateTime该类处理诸如夏令时之类的异常情况。

ZonedDateTime zdtLater = zdt.plusHours( 2 );

Duration

Duration

You can represent that two hours as an object.

您可以将那两个小时表示为一个对象。

Duration d = Duration.ofHours( 2 ) ;
ZonedDateTime zdtLater = zdt.plus( d ) ;