use*_*602 29 java datetime android
我一直在开发使用此代码的Android应用程序:
Date d=new Date(new Date().getTime()+28800000);
String s=new SimpleDateFormat("dd/MM/yyyy hh:mm:ss").format(d);
Run Code Online (Sandbox Code Playgroud)
我需要在当前时刻8小时后得到日期,我希望这个日期有24小时格式,但我不知道如何通过SimpleDateFormat来实现它.我还需要日期DD/MM/YYYY HH:MM:SS格式.
Tim*_*kov 65
12小时格式:
SimpleDateFormat simpleDateFormatArrivals = new SimpleDateFormat("hh:mm", Locale.UK);
Run Code Online (Sandbox Code Playgroud)
24小时格式:
SimpleDateFormat simpleDateFormatArrivals = new SimpleDateFormat("HH:mm", Locale.UK);
Run Code Online (Sandbox Code Playgroud)
vik*_*iii 56
这将以24小时格式为您提供日期.
Date date = new Date();
date.setHours(date.getHours() + 8);
System.out.println(date);
SimpleDateFormat simpDate;
simpDate = new SimpleDateFormat("kk:mm:ss");
System.out.println(simpDate.format(date));
Run Code Online (Sandbox Code Playgroud)
jee*_*eet 13
Date d=new Date(new Date().getTime()+28800000);
String s=new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(d);
Run Code Online (Sandbox Code Playgroud)
HH将持续0-23小时.
kk将返回1-24小时.
在此处查看更多:自定义格式
使用方法setIs24HourView(Boolean is24HourView)设置时间选择器以设置24小时视图.
现代方法使用java.time类。
Instant.now() // Capture current moment in UTC.
.truncatedTo( ChronoUnit.SECONDS ) // Lop off any fractional second.
.plus( 8 , ChronoUnit.HOURS ) // Add eight hours.
.atZone( ZoneId.of( "America/Montreal" ) ) // Adjust from UTC to the wall-clock time used by the people of a certain region (a time zone). Returns a `ZonedDateTime` object.
.format( // Generate a `String` object representing textually the value of the `ZonedDateTime` object.
DateTimeFormatter.ofPattern( "dd/MM/uuuu HH:mm:ss" )
.withLocale( Locale.US ) // Specify a `Locale` to determine the human language and cultural norms used in localizing the text being generated.
) // Returns a `String` object.
Run Code Online (Sandbox Code Playgroud)
23/01/2017 15:34:56
仅供参考, oldCalendar和Dateclasses 现在是legacy。由java.time类补充。java.time 的大部分内容都向后移植到 Java 6、Java 7 和 Android(见下文)。
Instant使用Instant类以 UTC 格式捕获当前时刻。
Instant instantNow = Instant.now();
Run Code Online (Sandbox Code Playgroud)
Instant.toString(): 2017-01-23T12:34:56.789Z
如果您只想要整秒,而没有任何一秒,请截断。
Instant instant = instantNow.truncatedTo( ChronoUnit.SECONDS );
Run Code Online (Sandbox Code Playgroud)
Instant.toString(): 2017-01-23T12:34:56Z
该Instant级可以做数学题,增加的时间量。指定由ChronoUnit枚举添加的时间量,TemporalUnit.
instant = instant.plus( 8 , ChronoUnit.HOURS );
Run Code Online (Sandbox Code Playgroud)
Instant.toString(): 2017-01-23T20:34:56Z
ZonedDateTime要通过特定区域挂钟时间的镜头查看同一时刻,请应用 aZoneId以获取ZonedDateTime.
以、、 或等格式指定正确的时区名称。永远不要使用 3-4 个字母的缩写,例如或因为它们不是真正的时区,不是标准化的,甚至不是唯一的(!)。continent/regionAmerica/MontrealAfrica/CasablancaPacific/AucklandESTIST
ZoneId z = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = instant.atZone( z );
Run Code Online (Sandbox Code Playgroud)
zdt.toString(): 2017-01-23T15:34:56-05:00[美国/蒙特利尔]
您可以通过在DateTimeFormatter对象中指定格式模式来生成所需格式的字符串。
请注意,大小写在格式模式的字母中很重要。这个问题的代码必须hh是12小时的时间,而大写的HH是在这两个24小时的时间(0-23)java.time.DateTimeFormatter以及传统java.text.SimpleDateFormat。
java.time 中的格式化代码与遗留中的类似,SimpleDateFormat但不完全相同。仔细研究班级文件。在这里,HH恰好工作相同。
DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MM/uuuu HH:mm:ss" ).withLocale( Locale.US );
String output = zdt.format( f );
Run Code Online (Sandbox Code Playgroud)
而不是硬编码的格式设置模式,考虑让java.time完全本地化的产生String通过调用文本DateTimeFormatter.ofLocalizedDateTime。
顺便说一句,请注意时区,并且Locale彼此无关;正交问题。一个是关于内容,意义(挂钟时间)。另一个是关于呈现,确定用于向用户呈现该含义的人类语言和文化规范。
Instant instant = Instant.parse( "2017-01-23T12:34:56Z" );
ZoneId z = ZoneId.of( "Pacific/Auckland" ); // Notice that time zone is unrelated to the `Locale` used in localizing.
ZonedDateTime zdt = instant.atZone( z );
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.FULL )
.withLocale( Locale.CANADA_FRENCH ); // The locale determines human language and cultural norms used in generating the text representing this date-time object.
String output = zdt.format( f );
Run Code Online (Sandbox Code Playgroud)
Instant.toString(): 2017-01-23T12:34:56Z
zdt.toString(): 2017-01-24T01:34:56+13:00[太平洋/奥克兰]
输出:狂欢节 24 janvier 2017 à 01:34:56 heure avancée de la Nouvelle-Zélande
该java.time框架是建立在Java 8和更高版本。这些类取代了麻烦的旧的遗留日期时间类,例如java.util.Date, Calendar, & SimpleDateFormat。
现在处于维护模式的Joda-Time项目建议迁移到java.time类。
要了解更多信息,请参阅Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。规范是JSR 310。
您可以直接与您的数据库交换java.time对象。使用符合JDBC 4.2或更高版本的JDBC 驱动程序。不需要字符串,不需要类。java.sql.*
从哪里获得 java.time 类?
更新:Joda-Time项目现在处于维护模式,团队建议迁移到java.time类。
Joda-Time使这种工作变得更加容易。
Instant.now() // Capture current moment in UTC.
.truncatedTo( ChronoUnit.SECONDS ) // Lop off any fractional second.
.plus( 8 , ChronoUnit.HOURS ) // Add eight hours.
.atZone( ZoneId.of( "America/Montreal" ) ) // Adjust from UTC to the wall-clock time used by the people of a certain region (a time zone). Returns a `ZonedDateTime` object.
.format( // Generate a `String` object representing textually the value of the `ZonedDateTime` object.
DateTimeFormatter.ofPattern( "dd/MM/uuuu HH:mm:ss" )
.withLocale( Locale.US ) // Specify a `Locale` to determine the human language and cultural norms used in localizing the text being generated.
) // Returns a `String` object.
Run Code Online (Sandbox Code Playgroud)
运行时…
Instant instantNow = Instant.now();
Run Code Online (Sandbox Code Playgroud)
请注意,此语法使用默认时区。更好的做法是使用显式 DateTimeZone 实例。
试试下面的代码
String dateStr = "Jul 27, 2011 8:35:29 PM";
DateFormat readFormat = new SimpleDateFormat( "MMM dd, yyyy hh:mm:ss aa");
DateFormat writeFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
Date date = null;
try {
date = readFormat.parse( dateStr );
} catch ( ParseException e ) {
e.printStackTrace();
}
String formattedDate = "";
if( date != null ) {
formattedDate = writeFormat.format( date );
}
System.out.println(formattedDate);
Run Code Online (Sandbox Code Playgroud)
祝你好运!!!
检查各种格式。
| 归档时间: |
|
| 查看次数: |
82607 次 |
| 最近记录: |