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
adding hours in date time format 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.Date
is deprecated, you should use java.util.Calendar
instead.
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.Date
into java.util.Calendar
Object and use Calendar.add()method to add Hours
转换java.util.Date
成java.util.Calendar
Object 并使用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 LocalTime
class.
对于仅限时间的值,请使用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 Instant
class 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 TimeUnit
class 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 ZoneId
to get a ZonedDateTime
object.
要查看某个社区的挂钟时间,请应用时区。应用 aZoneId
来获取一个ZonedDateTime
对象。
ZoneId z = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = instant.atZone( z );
You can add hours. The ZonedDateTime
class 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 ) ;