重新格式化自定义日期字符串

huangapple go评论73阅读模式
英文:

Reformat custom date from string

问题

I‘m fetching dates which have the format yyyy-MM-dd.
I have these extracted as a string which would look like this:

String date = "2020-09-05";

Now I want to convert it into a EEE, d MMM yyyy format, so the result should be: Sat, 5 Sep 2020.
I tried reformatting it like this:

Date convertedDate = new SimpleDateFormat(EEE, d MMM yyyy).parse(date);

For some reason this and many tries to get around it all end up with a java.text.ParseException: Unparseable date "2020-09-05".

Can‘t I convert a date from a string like that? Is there a better way to reformat a string into other formats?

英文:

I‘m fetching dates which have the format yyyy-MM-dd.
I have these extracted as a string which would look like this:

String date = "2020-09-05";

Now I want to convert it into a EEE, d MMM yyyy format, so the result should be: Sat, 5 Sep 2020.
I tried reformatting it like this:

Date convertedDate = new SimpleDateFormat(EEE, d MMM yyyy).parse(date);

For some reason this and many tries to get around it all end up with a java.text.ParseException: Unparseable date "2020-09-05".

Can‘t I convert a date from a string like that? Is there a better way to reformat a string into other formats?

答案1

得分: 3

你需要区分解析日期(将String转换为DateLocalDate类)和格式化日期(将DateLocalDate类实例转换为String)。

要解析初始字符串,请使用:

LocalDate convertedDate = LocalDate.parse(date);

要对convertedDate进行格式化,请使用:

String formattedDate = convertedDate.format(DateTimeFormatter.ofPattern("EEE, d MMM yyyy"));

编辑:我假设您至少在使用Java 8,因此可以使用更新的日期/时间类。有关更多信息,请参阅 https://www.baeldung.com/java-8-date-time-intro

英文:

You need to make a distinction between parsing the date (turning a String into a Date or LocalDate class) and formatting a date (turning the Date or LocalDate class instance to a String).

To parse the initial string, use:

LocalDate convertedDate = LocalDate.parse(date)

To format the convertedDate, use:

String formattedDate = convertedDate.format(DateTimeFormatter.ofPattern("EEE, d MMM yyyy"))

EDIT: I am assuming you are at least on Java 8, so you can use the newer date/time classes. See https://www.baeldung.com/java-8-date-time-intro for some more info.

答案2

得分: 1

你混淆了解析和格式化:你的代码试图根据格式来解析一个字符串。你告诉 Java 你期望日期包含星期名称。

你需要首先按照它当前的格式解析你的日期。这将给你一个日期。然后你需要用所需的格式格式化你的日期,这将给你一个字符串

final String dateString = "2020-09-05";
final Date date = new SimpleDateFormat("y-M-d").parse(dateString);
final String converted = new SimpleDateFormat("E, d MMM y").format(date);
英文:

You are mixing up parsing and formatting: your code is trying to parse a string given the format. You’re telling Java you’re expecting the date to contain the week name.

You need to first parse your date in the format it’s in. This will give you a date. You next need format your date with the desired format, which will give you a string:

final String dateString = "2020-09-05";
final Date date = new SimpleDateFormat("y-M-d").parse(dateString);
final String converted = new SimpleDateFormat("E, d MMM y").format(date);

答案3

得分: 0

以下是代码的翻译部分:

String sDate1 = "1998-12-31";
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse(sDate1);

System.out.println("EEE, d MMM yyyy 格式的日期: " + new SimpleDateFormat("EEE, d MMM yyyy").format(date));
英文:

ok here's the code:

String sDate1="1998-12-31";
		DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf.parse(sDate1);
 
        System.out.println("EEE, d MMM yyyy formatted date : " + new SimpleDateFormat("EEE, d MMM yyyy").format(date));

huangapple
  • 本文由 发表于 2020年9月6日 00:43:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/63756271.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定