Joh*_*hat 3 java datetime date android-studio
我有以下日期,
24/04/2019
13/05/2019
12/04/2019
Run Code Online (Sandbox Code Playgroud)
我想删除年份并将这些日期转换为以下格式,
24/04
13/05
12/04
Run Code Online (Sandbox Code Playgroud)
这不仅仅用于输出我想在该日期执行添加或申请循环.
MonthDay // Represent a month & day-of-month, without a year.
.from( // Extract a `MonthDay` from a `LocalDate`, omitting the year but keeping the month and day-of-month.
LocalDate // Represent a date-only value, without time-of-day and without time zone.
.parse( // Parse a string to get a `LocalDate`.
"24/04/2019" , // FYI, better to use standard ISO 8601 formats rather than devising a custom format such as this.
DateTimeFormatter.ofPattern( "dd/MM/uuuu" ) // Match the formatting pattern of your input strings.
) // Returns a `LocalDate`.
) // Returns a `MonthDay`.
.format( // Generate a `String` with text representing the value of this `MonthDay` object.
DateTimeFormatter.ofPattern( "dd/MM" )
) // Returns a `String`.
Run Code Online (Sandbox Code Playgroud)
请参阅IdeOne.com上的此代码.
24/04
LocalDate & MonthDayJava内置了这个工作的类:
解析输入字符串以获取LocalDate对象.定义格式模式以匹配输入格式.
String input = "24/04/2019" ;
DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MM/uuuu" ) ;
Run Code Online (Sandbox Code Playgroud)
解析.
LocalDate ld = LocalDate.parse( input , f ) ;
Run Code Online (Sandbox Code Playgroud)
生成标准ISO 8601格式的字符串YYYY-MM-DD.我强烈建议您在将日期时间值作为文本交换时使用这些格式,而不是设计自己的自定义格式.
在解析/生成字符串时,java.time类默认使用这些标准格式.因此无需指定格式化模式.
String output = ld.toString() ;
Run Code Online (Sandbox Code Playgroud)
2019年4月24日
只提取月份和日期,而忽略年份.
MonthDay md = MonthDay.from( ld ) ; // Extract the month & day-of-month, but omit the year.
Run Code Online (Sandbox Code Playgroud)
生成标准ISO 8601格式的字符串.格式在前面使用双连字符表示缺少的年份.
String output = md.toString():
Run Code Online (Sandbox Code Playgroud)
--04-24
您也可以解析此值.
MonthDay md = MonthDay.parse( "--04-24" ) ;
Run Code Online (Sandbox Code Playgroud)
你可以LocalDate通过指定一年来回到a.
LocalDate ld = md.atYear( 2019 ) ; // Generate a `LocalDate` by specifying a year to go with the month & day.
Run Code Online (Sandbox Code Playgroud)
不仅仅是输出我想在那个日期执行添加或申请循环.
研究这两个类的JavaDoc.提供了许多方便的方法.
你可以和isBefore和比较isAfter.
您可以进行数学运算,添加或减去时间跨度.您可以进行调整,例如跳转到特定的某个日期.请注意,java.time类遵循不可变对象模式.因此,不是更改("改变")原始对象,而是使用基于原始值的值创建第二个对象.
该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类?
该ThreeTen-额外项目与其他类扩展java.time.该项目是未来可能添加到java.time的试验场.您可以在此比如找到一些有用的类Interval,YearWeek,YearQuarter,和更多.
您可以获取索引 0 到 4 之间的子字符串:
String input = "24/04/2019";
String output = input.substring(0, 5);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
391 次 |
| 最近记录: |