Android-Calendar.getInstance的小时数值设置为零
我试图在java代码中查找当前时间Android-Calendar.getInstance的小时数值设置为零,android,android-calendar,Android,Android Calendar,我试图在java代码中查找当前时间 Calendar cal = Calendar.getInstance(); Date currentLocalTime = cal.getTime(); DateFormat date = new SimpleDateFormat("HH:mm a"); String localTime = date.format(currentLocalTime); ((TextView) (mainView.findViewById(R.
Calendar cal = Calendar.getInstance();
Date currentLocalTime = cal.getTime();
DateFormat date = new SimpleDateFormat("HH:mm a");
String localTime = date.format(currentLocalTime);
((TextView) (mainView.findViewById(R.id.l1))).setText(localTime);
由于某些原因,textview显示的是上午01:08而不是晚上8:08。您可以使用以下代码:
Date currentDate = Calendar.getInstance(TimeZone.getDefault()).getTime()
看看另一个问题,这可能是你问题的答案。
您可以使用以下代码:
Date currentDate = Calendar.getInstance(TimeZone.getDefault()).getTime()
看看另一个问题,这可能是你问题的答案。
如果您想要12小时的时间(晚上8:08而不是20:08),请确保将您的HH更改为HH,如下所示:
DateFormat date = new SimpleDateFormat("hh:mm a");
这并不能解释为什么你会看到凌晨1:08,但这只是一个开始。我在我的系统上尝试了完全相同的代码,时间显示正确。所以,我不确定你的问题在哪里。我的猜测是,您的系统时钟设置不正确,或者更有可能是您的日历和日期库出现了奇怪的问题。如果您继续遇到问题,我建议您转到图书馆。它非常棒,它修复了在使用日期和日历库时遇到的所有小问题 如果您想要12小时的时间(8:08 PM而不是20:08 PM),请确保将您的HH更改为HH,如下所示:
DateFormat date = new SimpleDateFormat("hh:mm a");
这并不能解释为什么你会看到凌晨1:08,但这只是一个开始。我在我的系统上尝试了完全相同的代码,时间显示正确。所以,我不确定你的问题在哪里。我的猜测是,您的系统时钟设置不正确,或者更有可能是您的日历和日期库出现了奇怪的问题。如果您继续遇到问题,我建议您转到图书馆。它非常棒,它修复了在使用日期和日历库时遇到的所有小问题 tl;博士
正如Don Brody在中所述,您的格式模式不正确,使用了HH
(适用于24小时制)而本应是小写的HH
(适用于12小时制)。您可能还存在JVM当前默认时区未设置为预期时区的问题
还有…你的问题没有意义。您使用的是几年前被java.time取代的糟糕的旧类
LocalTime // Represent time-of-day without date and without time zone.
.now() // Capture the current time-of-day as seen in the JVM’s current default time zone. Better to pass the optional `ZoneId` argument to specify explicitly the desired/expected time zone.
.format( // Generate a `String` representing our time-of-day value.
DateTimeFormatter
.ofLocalizedTime( FormatStyle.SHORT ) // Automatically localize rather than hard-code a specific formatting pattern.
.withLocale( Locale.US ) // Locale determines the human language and cultural norms used in localizing.
) // Returns a `String` object.
晚上10:09
使用java.time
您使用的是现在被java.time类取代的糟糕的旧类
获取一天中的当前时间
LocalTime lt = LocalTime.now() ; // Capture the current time-of-day using the JVM’s current default time zone.
与隐式依赖JVM的当前默认时区相比,显式声明所需/预期时区更好
ZoneId z = ZoneId.of( "America/Montreal" ) ;
LocalTime lt = LocalTime.now( z ) ;
生成AM-PM格式的字符串
DateTimeFormatter f = DateTimeFormatter.ofPattern( "hh:mm a" ) ; // Lowercase `hh` for 12-hour clock, uppercase `HH` for 24-hour clock.
String output = lt.format( f ) ;
更好的是,让java.time为您自动本地化
Locale locale = Locale.US ;
DateTimeFormatter f2 = DateTimeFormatter.ofLocalizedTime( FormatStyle.SHORT ).withLocale( locale ) ;
String output2 = lt.format( f2 ) ;
你看
lt.toString():22:09:19.825
输出:晚上10:09
输出2:10:09下午
关于java.time 该框架内置于Java8及更高版本中。这些类取代了麻烦的旧日期时间类,例如,& 该项目现已启动,建议迁移到类 要了解更多信息,请参阅。并搜索堆栈溢出以获得许多示例和解释。规格是 您可以直接与数据库交换java.time对象。使用兼容的或更高版本。不需要字符串,也不需要
java.sql.*
类
从哪里获得java.time类
- 然后
- 内置的李>
- 标准JavaAPI的一部分,带有捆绑实现
- Java9添加了一些次要功能和修复
- 及
- 大部分java.time功能都在中向后移植到Java6和Java7
-
- 更高版本的Android捆绑包实现了java.time类
- 对于早期的Android(tl;dr
正如Don Brody在中所述,您的格式化模式不正确,使用了
(适用于24小时制)而它应该是小写的HH
(适用于12小时制)。您可能还存在JVM当前默认时区未设置为预期时区的问题 另外,您的问题是没有意义的。您使用的是几年前被java.time取代的糟糕的旧类HH
晚上10:09 使用java.time 您使用的是现在被java.time类取代的糟糕的旧类 获取一天中的当前时间LocalTime // Represent time-of-day without date and without time zone. .now() // Capture the current time-of-day as seen in the JVM’s current default time zone. Better to pass the optional `ZoneId` argument to specify explicitly the desired/expected time zone. .format( // Generate a `String` representing our time-of-day value. DateTimeFormatter .ofLocalizedTime( FormatStyle.SHORT ) // Automatically localize rather than hard-code a specific formatting pattern. .withLocale( Locale.US ) // Locale determines the human language and cultural norms used in localizing. ) // Returns a `String` object.
与隐式依赖JVM的当前默认时区相比,显式声明所需/预期时区更好LocalTime lt = LocalTime.now() ; // Capture the current time-of-day using the JVM’s current default time zone.
生成AM-PM格式的字符串ZoneId z = ZoneId.of( "America/Montreal" ) ; LocalTime lt = LocalTime.now( z ) ;
更好的是,让java.time为您自动本地化DateTimeFormatter f = DateTimeFormatter.ofPattern( "hh:mm a" ) ; // Lowercase `hh` for 12-hour clock, uppercase `HH` for 24-hour clock. String output = lt.format( f ) ;
你看 lt.toString():22:09:19.825 输出:晚上10:09 输出2:10:09下午Locale locale = Locale.US ; DateTimeFormatter f2 = DateTimeFormatter.ofLocalizedTime( FormatStyle.SHORT ).withLocale( locale ) ; String output2 = lt.format( f2 ) ;
关于java.time 该框架内置于Java8及更高版本中。这些类取代了麻烦的旧日期时间类,如,& 该项目现已启动,建议迁移到类 要了解更多信息,请参阅.和搜索堆栈溢出以获取许多示例和解释。规范为 您可以直接与数据库交换java.time对象。使用兼容的或更高版本。不需要字符串,也不需要
classes 从哪里获得java.time类java.sql.*
- 然后
- 内置的
- 标准JavaAPI的一部分,带有捆绑实现
- Java9添加了一些次要功能和修复
- 及
- 大部分java.time功能都在中向后移植到Java6和Java7
-
- 更高版本的Android捆绑包实现了java.time类
- 对于早期的Android(是否始终获取特定时区中的当前时间?仅供参考,诸如
、java.util.date
、和java.util.Calendar
等麻烦的旧日期时间类现在是遗留的,被这些类所取代。java.time的大部分功能在未来都会重新移植到java 6和java 7中。)项目。进一步适应项目中早期的Android。参见。零还是一?你的标题是“小时值设置为零”,但是java.text.SimpleDateFormat
- 然后