简体   繁体   English

字符串到本地日期

[英]String to LocalDate

How can i convert a string to a LocalDate ?如何将字符串转换为LocalDate

I have seen examples like:我见过这样的例子:

LocalDate dt = new LocalDate("2005-11-12");

But my strings are like:但我的字符串是这样的:

2005-nov-12

java.time时间

Since Java 1.8, you can achieve this without an extra library by using the java.time classes.从 Java 1.8 开始,您可以使用java.time类在没有额外库的情况下实现这一点。 See Tutorial .请参阅教程

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MMM-dd");
formatter = formatter.withLocale( putAppropriateLocaleHere );  // Locale specifies human language for translating, and cultural norms for lowercase/uppercase and abbreviations and such. Example: Locale.US or Locale.CANADA_FRENCH
LocalDate date = LocalDate.parse("2005-nov-12", formatter);

The syntax is nearly the same though.语法几乎相同。

As you use Joda Time, you should use DateTimeFormatter :当您使用 Joda Time 时,您应该使用DateTimeFormatter

final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MMM-dd");
final LocalDate dt = dtf.parseLocalDate(yourinput);

If using Java 8 or later, then refer to hertzi's answer如果使用 Java 8 或更高版本,请参考hertzi 的回答

You may have to go from DateTime to LocalDate.您可能必须从 DateTime 转到 LocalDate。

Using Joda Time:使用 Joda 时间:

DateTimeFormatter FORMATTER = DateTimeFormat.forPattern("yyyy-MMM-dd");
DateTime dateTime = FORMATTER.parseDateTime("2005-nov-12");
LocalDate localDate = dateTime.toLocalDate();

Datetime formatting is performed by the org.joda.time.format.DateTimeFormatter class .日期时间格式化由org.joda.time.format.DateTimeFormatter class执行。 Three classes provide factory methods to create formatters , and this is one.三个类提供了创建格式化程序的工厂方法,这是一个。 The others are ISODateTimeFormat and DateTimeFormatterBuilder .其他的是ISODateTimeFormatDateTimeFormatterBuilder

DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MMM-dd");
LocalDate lDate = new LocalDate().parse("2005-nov-12",format);

final org.joda.time.LocalDate class is an immutable datetime class representing a date without a time zone . final org.joda.time.LocalDate class是一个不可变的日期时间类,表示没有时区的日期。 LocalDate is thread-safe and immutable , provided that the Chronology is as well. LocalDate线程安全LocalDate ,前提是Chronology也是如此。 All standard Chronology classes supplied are thread-safe and immutable.提供的所有标准 Chronology 类都是线程安全且不可变的。

DateTimeFormatter has in-built formats that can directly be used to parse a character sequence. DateTimeFormatter具有可直接用于解析字符序列的内置格式。 It is case Sensitive, Nov will work however nov and NOV wont work:区分大小写,Nov 将工作,但 nov 和 NOV 将无法工作:

DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy-MMM-dd");

try {
    LocalDate datetime = LocalDate.parse(oldDate, pattern);
    System.out.println(datetime); 
} catch (DateTimeParseException e) {
    // DateTimeParseException - Text '2019-nov-12' could not be parsed at index 5
    // Exception handling message/mechanism/logging as per company standard
}

DateTimeFormatterBuilder provides custom way to create a formatter. DateTimeFormatterBuilder提供自定义方式来创建格式化程序。 It is Case Insensitive, Nov , nov and NOV will be treated as same.它不区分大小写,Nov ,nov 和 NOV 将被视为相同。

DateTimeFormatter f = new DateTimeFormatterBuilder().parseCaseInsensitive()
        .append(DateTimeFormatter.ofPattern("yyyy-MMM-dd")).toFormatter();
try {
    LocalDate datetime = LocalDate.parse(oldDate, f);
    System.out.println(datetime); // 2019-11-12
} catch (DateTimeParseException e) {
     // Exception handling message/mechanism/logging as per company standard
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM