Java:CALENDAR.HOUR不返回我设置的值?
我不是一个专业的程序员,但我已经在网上寻找解决方案,但找不到任何帮助 以下是我在工作中为自动化项目所做的工作。给我一个以毫秒为单位的时间值。我需要获取该时间值,加上6分钟,然后检索小时、分钟和AM_PM值,这样我就可以进行测试 问题是,在我检索时间之后,我用日历设置时间,进行添加,当我检索分钟和小时时,它们设置不正确 例如,以下是我的代码:Java:CALENDAR.HOUR不返回我设置的值?,java,Java,我不是一个专业的程序员,但我已经在网上寻找解决方案,但找不到任何帮助 以下是我在工作中为自动化项目所做的工作。给我一个以毫秒为单位的时间值。我需要获取该时间值,加上6分钟,然后检索小时、分钟和AM_PM值,这样我就可以进行测试 问题是,在我检索时间之后,我用日历设置时间,进行添加,当我检索分钟和小时时,它们设置不正确 例如,以下是我的代码: _logger.info("Get current system time in milliseconds"); long currentT
_logger.info("Get current system time in milliseconds");
long currentTime = TimeApi.getCurrentSystemTime(_deviceUnderTestPIN);
_logger.info("Current device time is : " + Long.toString(currentTime));
_logger.info("Set Calendar object with device time");
Calendar now = Calendar.getInstance();
now.setTimeInMillis(currentTime);
long timeSet = now.getTimeInMillis();
_logger.info("Calendar object is set to : " + Long.toString(timeSet));
_logger.info("add mintuesToAdd to current time");
now.add(Calendar.MINUTE, minutesToAdd);
long timeAdd = now.getTimeInMillis();
_logger.info("Calendar Time after Add: " + Long.toString(timeAdd));
_logger.info("set hour and minute");
// if the hour is 12am or 12pm the Calendar object will return 0, we need to pass in 12 so we will set it to 12 below if it returns 0
hour = now.get(Calendar.HOUR);
if (hour == 0) {
hour = 12;
}
minutes = now.get(Calendar.MINUTE);
_logger.info("set amPM");
if (now.get(Calendar.AM_PM) == 0) {
amPM = false;
} else {
amPM = true;
}
_logger.info("Setting alarm hour to: " + Integer.toString(hour));
_logger.info("Setting the alarm minutes to: " + Integer.toString(minutes));
_logger.info("Setting alarm AM_PM to: " + Boolean.toString(amPM));
下面是我的测试运行的输出:
2013-06-06 13:15:36.007 INFO Current device time is : 1370524535000
2013-06-06 13:15:36.007 INFO Set Calendar object with device time
2013-06-06 13:15:36.007 INFO Calendar object is set to : 1370524535000
2013-06-06 13:15:36.007 INFO add mintuesToAdd to current time
2013-06-06 13:15:36.007 INFO Calendar Time after Add: 1370524895000
2013-06-06 13:15:36.007 INFO set hour and minute
2013-06-06 13:15:36.007 INFO set amPM
2013-06-06 13:15:36.023 INFO Setting alarm hour to: 1
2013-06-06 13:15:36.023 INFO Setting the alarm minutes to: 21
2013-06-06 13:15:36.023 INFO Setting alarm AM_PM to: true
正如您所看到的,我拥有的时间值正在尝试设置为2013年6月6日星期四09:15:35 GMT-0400(东部夏令时)。所以我不明白的是,为什么我现在打电话时要占用服务器时间。get(Calendar.HOUR)
如果您有任何帮助,我们将不胜感激。我认为您在获取日历实例时需要使用timezome参数,如下所示:
Calendar now = Calendar.getInstance(TimeZone.getTimeZone("America/New_York"));
tl;博士
下午6:51:35
java.time
您使用的是麻烦的旧日期时间类,它们现在是遗留的,被java.time类取代
如果给定自UTC 1970年第一时刻(1970-01-01T00:00Z)的历元参考起的毫秒计数,则解析为瞬间
。该类表示时间线上的一个时刻,分辨率为(小数点的九(9)位)
instant.toString():2013-06-06T13:15:35Z
将您的六分钟时间跨度表示为持续时间
Duration d = Duration.ofMinutes( 6 ) ; // Span of time (hours-minutes-seconds) unattached to the timeline.
算算
Instant sixMinutesLater = instant.plus( d ) ; // Add the duration of six minutes to the UTC moment.
转换到更灵活的,以完成我们的其他工作
OffsetDateTime odt = OffsetDateTime.ofInstant( sixMinutesLater , ZoneOffset.UTC ) ;
要获取一天中没有日期和时区的时间,请提取一个
若要询问是否为上午,请将其与另一个表示中午12:00的LocalTime
进行比较
Boolean isBeforeNoon = lt.isBefore( LocalTime.NOON ) ;
您可以查询以下部分:getHour
(0-23)、getMinute
,等等
您可能希望生成一个表示此值的字符串。指定格式模式,或自动本地化
DateTimeFormatter f = DateTimeFormatter.ofLocalizedTime( FormatStyle.MEDIUM ).withLocale( Locale.US ) ;
String output = lt.format( f ) ;
以上代码假定您希望在中工作。相反,如果您希望某个地区的人在挂钟上看到一天中的时间,则必须指定时区(ZoneId
)以获取zoneDateTime
对象
ZoneId z = ZoneId.of( "Africa/Tunis" );
ZonedDateTime zdtAfricaTunis = instant.atZone( z );
LocalTime ltAfricaTunis = zdtAfricaTunis.toLocalTime();
转储到控制台
System.out.println( "instant.toString(): " + instant + " and sixMinutesLaterInstant: " + sixMinutesLaterInstant );
System.out.println( "odt.toString(): " + odt + " and lt.toString(): " + lt + " is morning: " + isBeforeNoon + " with output: " + output );
System.out.println( "zdtAfricaTunis.toString(): " + zdtAfricaTunis + " and ltAfricaTunis.toString(): " + ltAfricaTunis );
instant.toString():2013-06-06T13:15:35Z和六分钟后instant:2013-06-06T13:21:35Z
odt.toString():2013-06-06T13:21:35Z和lt.toString():14:21:35是晨:假,输出为下午1:21:35
zdtAfricaTunis.toString():2013-06-06T14:21:35+01:00[非洲/突尼斯]和意大利突尼斯。toString():14:21:35
关于java.time 该框架内置于Java8及更高版本中。这些类取代了麻烦的旧日期时间类,例如,& 该项目现已启动,建议迁移到类 要了解更多信息,请参阅。并搜索堆栈溢出以获得许多示例和解释。规格是 从哪里获得java.time类
- ,及以后
- 内置的李>
- 标准JavaAPI的一部分,带有捆绑实现
- Java9添加了一些次要功能和修复
- 及
- 大部分java.time功能都在中向后移植到Java6和Java7
-
- 更高版本的Android捆绑包实现了java.time类
- 对于早期的Android,该项目采用了ThreeTen Backport(如上所述)。看
该项目使用其他类扩展了java.time。这个项目是java.time将来可能添加的一个试验场。您可以在这里找到一些有用的课程,如、、和。您如何知道时间不对?在进行操作之前,您从不记录日历的值。此外,您应该记录整个日历实例(即所有字段)或使用时间格式化程序查看所有字段。否则,滚动效果(即将7分钟增加到9:55会改变小时数)会让您感到困惑。
现在。添加也会改变小时数。使用新的SimpleDataFormat(“HH:mm”)
输出日历实例。。。为了方便,你可以使用时间单位。使用日历时,我经常会头发花白,所以我避免使用日历;)对不起,我不知道你的意思。我已经在添加6分钟之前和之后以毫秒为单位输出了时间。查看输出的第三行和第五行,值已经更改了6分钟。你的问题是什么?另外,在添加时间之前输出小时和分钟,然后您将看到更改。这是有道理的,但是我无法硬编码时区,因为它可能会更改,我需要从TimeApi.getCurrentSystemTime(此方法返回设备Im测试上的时间,而不是服务器)返回的值中检索它。我现在认为。setTimeInMillis(当前时间);还将设置时区value@user2460213setTimeInMillis在默认timezome中设置长值。我建议在使用它之前检索设备的时区并在日历中明确设置。引用JavaDocs,setTimeInMillis
从历元开始以UTC毫秒为单位获取新时间。@user2460213,如果您无法获取设备时区,那么我认为另一种选择是获取本地时间和设备时间之间的差异(以毫秒为单位),例如TimeApi.getCurrentSystemTime(_deviceUnderTestPIN)-new Date().getTime();并将其添加到日历中。
DateTimeFormatter f = DateTimeFormatter.ofLocalizedTime( FormatStyle.MEDIUM ).withLocale( Locale.US ) ;
String output = lt.format( f ) ;
ZoneId z = ZoneId.of( "Africa/Tunis" );
ZonedDateTime zdtAfricaTunis = instant.atZone( z );
LocalTime ltAfricaTunis = zdtAfricaTunis.toLocalTime();
System.out.println( "instant.toString(): " + instant + " and sixMinutesLaterInstant: " + sixMinutesLaterInstant );
System.out.println( "odt.toString(): " + odt + " and lt.toString(): " + lt + " is morning: " + isBeforeNoon + " with output: " + output );
System.out.println( "zdtAfricaTunis.toString(): " + zdtAfricaTunis + " and ltAfricaTunis.toString(): " + ltAfricaTunis );