字符串中的月-年-日期Java 8

字符串中的月-年-日期Java 8,java,java-8,Java,Java 8,破例 无法分析文本“03/2018”:无法从临时Accessor获取LocalDate: 如何使用Java8解析此字符串并将其转换为日期,Java8具有默认的每月第一天。我们使用的东西 DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("MM/yyyy"); LocalDate parsedDate = LocalDate.parse(entryOne.getKey(), dateFormat) 以下是对我有效的方法: Tem

破例

无法分析文本“03/2018”:无法从临时Accessor获取LocalDate:

如何使用Java8解析此字符串并将其转换为日期,Java8具有默认的每月第一天。我们使用的东西

DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("MM/yyyy");
LocalDate parsedDate = LocalDate.parse(entryOne.getKey(), dateFormat)

以下是对我有效的方法:

TemporalAdjusters.firstDayOfMonth()

以下是对我有效的方法:

TemporalAdjusters.firstDayOfMonth()

MM/yyyy
字符串转换为
LocalDate
,有两种选择:

String dateAsString = "03/2018";
DateTimeFormatter fmt = DateTimeFormat.forPattern("MM/yyyy");
DateTime dt = fmt.parseDateTime(dateAsString);
LocalDateTime ldt = new LocalDateTime(dt);
int dayOfWeek = ldt.getDayOfWeek(); //has value of 4 since Thursday was the first day of March
  • 解析为
    YearMonth
    ,然后转换为
    LocalDate

    String dateAsString = "03/2018";
    DateTimeFormatter fmt = DateTimeFormat.forPattern("MM/yyyy");
    DateTime dt = fmt.parseDateTime(dateAsString);
    LocalDateTime ldt = new LocalDateTime(dt);
    int dayOfWeek = ldt.getDayOfWeek(); //has value of 4 since Thursday was the first day of March
    
  • 使用定义了默认月日的
    DateTimeFormatter

    String date = "04/2018";
    DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("MM/yyyy");
    YearMonth yearMonth = YearMonth.parse(date, dateFormat);
    LocalDate parsedDate = yearMonth.atDay(1);
    System.out.println(parsedDate); // prints: 2018-04-01
    

MM/yyyy
字符串转换为
LocalDate
,有两种选择:

String dateAsString = "03/2018";
DateTimeFormatter fmt = DateTimeFormat.forPattern("MM/yyyy");
DateTime dt = fmt.parseDateTime(dateAsString);
LocalDateTime ldt = new LocalDateTime(dt);
int dayOfWeek = ldt.getDayOfWeek(); //has value of 4 since Thursday was the first day of March
  • 解析为
    YearMonth
    ,然后转换为
    LocalDate

    String dateAsString = "03/2018";
    DateTimeFormatter fmt = DateTimeFormat.forPattern("MM/yyyy");
    DateTime dt = fmt.parseDateTime(dateAsString);
    LocalDateTime ldt = new LocalDateTime(dt);
    int dayOfWeek = ldt.getDayOfWeek(); //has value of 4 since Thursday was the first day of March
    
  • 使用定义了默认月日的
    DateTimeFormatter

    String date = "04/2018";
    DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("MM/yyyy");
    YearMonth yearMonth = YearMonth.parse(date, dateFormat);
    LocalDate parsedDate = yearMonth.atDay(1);
    System.out.println(parsedDate); // prints: 2018-04-01
    

DateTimeFormatterBuilder#toFormatter(Locale.ENGLISH)是一种很好的做法。@SparkOn如果您建议使用类似于
.toFormatter(Locale.US)
,那么您也应该建议使用
.ofPattern(“MM/yyyy”,Locale.US)
,但无论您的默认语言环境如何,它都不会影响结果。现在,如果模式显示月份名称,则区域设置很重要,但由于只显示/分析数字,区域设置并不重要。DateTimeFormatterBuilder#toFormatter(locale.ENGLISH)是一种很好的做法。@SparkOn如果您建议使用类似
.toFormatter(locale.US)
,那么您还应该建议使用模式(“MM/yyyy”的
,Locale.US)
,但无论您的默认语言环境如何,它都不会影响结果。现在,如果模式显示月份名称,那么区域设置会很重要,但因为只显示/解析数字,所以区域设置不重要?提问是因为我认为问题使用了
java.time
,这可能被认为是Joda-time的继承者。让我猜猜,使用Joda-time?提问是因为我认为问题使用了
java.time
,这可能被认为是Joda time的继承者。