简体   繁体   English

如何将 2019 年 10 月 9 日 12:00:00:000AM 的日期字符串格式化为 dd/MM/yyyy

[英]How to format date string of Oct 9 2019 12:00:00:000AM to dd/MM/yyyy

I have date formatted as Oct 9 2019 12:00:00:000AM .我的日期格式为Oct 9 2019 12:00:00:000AM I want to change its format to 09/10/2019 .我想将其格式更改为09/10/2019

So far here is what i tried:到目前为止,这是我尝试过的:

    SimpleDateFormat outs= new SimpleDateFormat("dd/MM/yyyy");
    String newDate= outs.format(invoiceEntity.getTidate()); // where invoiceEntitygetTidate() is the date

But i get error但我得到错误

*java.lang.IllegalArgumentException: *java.lang.IllegalArgumentException:

Cannot format given Object as a Date*.无法将给定的 Object 格式化为日期*。

Use Java 8 date time API, you can parse the input string into LocalDate using DateTimeFormatter and then parse it to required format string使用 Java 8 日期时间 API,您可以使用DateTimeFormatter将输入字符串解析为LocalDate ,然后将其解析为所需的格式字符串

String date = "Oct 9 2019 12:00:00:000AM";

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM d yyyy hh:mm:ss:SSSa");

LocalDate local = LocalDate.parse(date,formatter);

System.out.println(local.format(DateTimeFormatter.ofPattern("dd/MM/yyy")));

Or if you have java.util.Date object, convert to java.time classes (specifically, Instant ) and proceed to get your LocalDate object. Or if you have java.util.Date object, convert to java.time classes (specifically, Instant ) and proceed to get your LocalDate object.

Date utilDate = new Date();

String output = utilDate.toInstant()
                        .atZone(ZoneId.systemDefault())
                        .toLocalDate()
                        .format(DateTimeFormatter.ofPattern("dd/MM/yyyy"));

System.out.println(output);

Java 8 introduced new APIs for Date and Time to address the shortcomings of the older java.util.Date and java.util.Calendar. Java 8 引入了新的日期和时间 API,以解决旧版 java.util.Date 和 java.util.Calendar 的缺点

Issues with the Existing Date/Time APIs现有日期/时间 API 的问题

  1. Thread Safety – The Date and Calendar classes are not thread safe线程安全 – Date 和 Calendar 类不是线程安全的
  2. APIs Design and Ease of Understanding – The Date and Calendar APIs are poorly designed with inadequate methods to perform day-to-day operations. API 设计和易于理解 - 日期和日历 API 设计不佳,执行日常操作的方法不足。
  3. onedDate and Time – Developers had to write additional logic to handle timezone logic with the old APIs, whereas with the new APIs, handling of timezone can be done with Local and ZonedDate/Time APIs. onedDate 和 Time – 开发人员必须编写额外的逻辑来使用旧 API 处理时区逻辑,而使用新 API,可以使用 Local 和 ZonedDate/Time API 来处理时区。

Fr more idea please refer here更多想法请参考这里

You can use the JAVA 8 Date API feature for converting the date.您可以使用JAVA 8 Date API功能来转换日期。 Example is given below.下面给出示例。

String oldDate = "Oct 9 2019 12:00:00:000AM";

// mentioning the input date format
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM d yyyy hh:mm:ss:SSSa");

//converting the date
LocalDate localDate = LocalDate.parse(oldDate,formatter);

//Final date formatted 
String formattedDate=localDate.format(DateTimeFormatter.ofPattern("dd/MM/yyy"));

System.out.println("Old Date :- "+ oldDate);
System.out.println("New Date :- "+formattedDate);

Output Output

Old Date:- Oct 9 2019 12:00:00:000AM旧日期:- 2019 年 10 月 9 日 12:00:00:000AM

New Date:- 09/10/2019新日期:- 09/10/2019

暂无
暂无

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

相关问题 如何解析包含格式“Oct 1, 2015 12:00:00 AM”的日期的字符串? - How to parse a string which holds date of format "Oct 1, 2015 12:00:00 AM"? 这个“YYYY-MM-DD 00:00:00+00:00”是哪种Java日期格式? - Which Java Date format is this “YYYY-MM-DD 00:00:00+00:00”? 格式化2015年6月28日T12:00:00 AM的Java代码为日期格式yyyy-MM-dd'T'HH:mm:ss - java code to format Jun 28, 2015T12:00:00 AM to date format yyyy-MM-dd'T'HH:mm:ss 如何使用日期类获取格式为“ YYYY-MM-DD 00:00:00.0”的数据? - How can i get data in format “YYYY-MM-DD 00:00:00.0” using class Date? 如何将“ 2019-04-11T05:00:54.000 + 01:00”转换为dd / MM / yyyy hh:mm格式 - How to convert “2019-04-11T05:00:54.000+01:00” into dd/MM/yyyy hh:mm format 如何将“2012-03-04 00:00:00.0”转换为日期格式为“dd-mm-yyyy HH:mm:ss”使用Java - How to convert “2012-03-04 00:00:00.0” to Date with Format “dd-mm-yyyy HH:mm:ss” Using Java 如何以hh:mm格式在dd / mm / yyyy中格式化此字符串“ 2015-06-22T09:40:30 + 01:00”? - How to format this String “2015-06-22T09:40:30+01:00” in dd/MM/yyyy at hh:mm format? 转换分析时间dd.MM.yyyy','12:00时出错 - Error converting parsing TIME dd.MM.yyyy', '12:00 java 日期格式将 'M/d/yyyy' 转换为 'yyyy-MM-dd'T'HH:mm:ss.SSSXXX' 就像 2021-04-05T00:00-07:00[UTC-07:00] - java Date format convert 'M/d/yyyy' to 'yyyy-MM-dd'T'HH:mm:ss.SSSXXX' like 2021-04-05T00:00-07:00[UTC-07:00] 如何将包含日期的字符串值格式化为yyyy-mm-dd? - how to format a string value containing date to yyyy-mm-dd?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM