如何在日期(dd / mm / yyyy)中删除年份(yyyy)

时间:2019-01-26 04:46:19

标签: java android-studio date datetime

我有以下日期

24/04/2019

13/05/2019

12/04/2019

我要删除年份并将这些日期转换为以下格式,

24/04

13/05

12/04

这不是仅用于输出,我想在该日期执行加法或申请循环。

4 个答案:

答案 0 :(得分:6)

tl; dr

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`.

请参阅此code run live at IdeOne.com

  

24/04

LocalDateMonthDay

Java内置了用于此工作的类:

解析您的输入字符串以获取LocalDate对象。定义格式模式以匹配输入格式。

String input = "24/04/2019" ;
DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MM/uuuu" ) ;

解析。

LocalDate ld = LocalDate.parse( input , f ) ;

生成标准ISO 8601格式的字符串YYYY-MM-DD。我强烈建议您在将日期时间值转换为文本时使用这些格式,而不要设计自己的自定义格式。

java.time 类在解析/生成字符串时默认使用这些标准格式。因此,无需指定格式设置模式。

String output = ld.toString() ; 
  

2019-04-24

仅提取月份和日期,而忽略年份。

MonthDay md = MonthDay.from( ld ) ;  // Extract the month & day-of-month, but omit the year.

Generate a string(标准ISO 8601格式)。该格式在前面使用双连字符表示缺少的年份。

String output = md.toString():
  

-04-24

您也可以解析该值。

MonthDay md = MonthDay.parse( "--04-24" ) ;

您可以通过指定年份返回到LocalDate

LocalDate ld = md.atYear( 2019 ) ;  // Generate a `LocalDate` by specifying a year to go with the month & day.
  

不只用于输出,我想在该日期执行加法或申请循环。

研究这两个类的JavaDoc。提供了许多方便的方法。

您可以与isBeforeisAfter进行比较。

您可以进行数学运算,增加或减少时间跨度。您可以进行调整,例如跳转到特定的月份。请注意, java.time 类遵循immutable objects模式。因此,创建第二个对象而不是更改(“变异”)原始对象,并使用基于原始对象的值。


关于 java.time

java.time框架已内置在Java 8及更高版本中。这些类取代了麻烦的旧legacy日期时间类,例如java.util.DateCalendarSimpleDateFormat

目前位于Joda-Timemaintenance mode项目建议迁移到java.time类。

要了解更多信息,请参见Oracle Tutorial。并在Stack Overflow中搜索许多示例和说明。规格为JSR 310

您可以直接与数据库交换 java.time 对象。使用符合JDBC driver或更高版本的JDBC 4.2。不需要字符串,不需要java.sql.*类。

在哪里获取java.time类?

ThreeTen-Extra项目使用其他类扩展了java.time。该项目为将来可能在java.time中添加内容提供了一个试验场。您可能会在这里找到一些有用的类,例如IntervalYearWeekYearQuartermore

答案 1 :(得分:4)

您可以从索引0到4获得子字符串:

String input = "24/04/2019";
String output = input.substring(0, 5);

答案 2 :(得分:2)

使用此:

String input = "24/04/2019";
String output = input.substring(0,input.lastIndexOf("/"));

答案 3 :(得分:0)

您还可以尝试使用以下字符串拆分逻辑。

String date="24/04/2019";

String [ ] ddyymm=date.split("/");

 String output=ddyymm[0]+"/"+ddmmyy[1];