Getting the date as per the format in which month name is coming as first

那年仲夏 提交于 2021-01-29 07:26:54

问题


I am getting the string in this formatas shown below

03-12-2018

I want to convert this into as below format as per Java 8 standards please advise

December 03 , 2018  

what I have tried is shown below but i was not succeessful , please advise how to acheieve the same

SimpleDateFormat month_date = new SimpleDateFormat("MMM yyyy", Locale.ENGLISH);
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    String actualDate = "03-12-2018";

    Date date = sdf.parse(actualDate);

    String month_name = month_date.format(date);
    System.out.println("Month :" + month_name);  

回答1:


java.time

    DateTimeFormatter originalFormatter = DateTimeFormatter.ofPattern("dd-MM-uuuu");
    DateTimeFormatter monthFirst = DateTimeFormatter
            .ofLocalizedDate(FormatStyle.LONG)
            .withLocale(Locale.ENGLISH);

    String actualDate = "03-12-2018";
    LocalDate date = LocalDate.parse(actualDate, originalFormatter);
    String monthName = date.format(monthFirst);
    System.out.println("Month :" + monthName);

Output:

Month :December 3, 2018

Since you are using Java 8 (and even if you didn’t), avoid the long outdated and notoriously troublesome SimpleDateFormat class. Use the built in formats where you can rather than rolling your own.

What went wrong in your code?

You parsed a string of 03-12-2018 with a format of yyyy-MM-dd. So this parses into the 2018th day of the 12th month of year 3 CE (2015 years ago). There obviously weren’t 2018 days in December. So it would have been fair to expect an exception. This is just one of the points where SimpleDateFormat is troublesome: with standard settings it just keeps counting days into the following months and years and ends up at June 9 year 9, that is, 5 and a half years later. Next you formatted this date with a formatter including month name and year, it seems you had forgot the day of month. Anyway it printed as Jun 0009 (which you should have told us in your question so that we could see what was wrong; this information can be very helpful in trying to solve your problem).

Link

Oracle tutorial: Date Time explaining how to use java.time.




回答2:


It's just a matter of choosing the correct format (and applying a correct Locale):

 DateTimeFormatter f = DateTimeFormatter.ofPattern("LLLL dd, yyyy");
 System.out.println(f.format(yourDate));

Btw it's in the documentation:

... Number/Text: If the count of pattern letters is 3 or greater, use the Text rules above. Otherwise use the Number rules above. ...




回答3:


Use the following code:

SimpleDateFormat month_date = new SimpleDateFormat("MMMM dd, yyyy", Locale.ENGLISH);
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");

String actualDate = "03-12-2018";

Date date = sdf.parse(actualDate);

String month_name = month_date.format(date);
System.out.println("Month :" + month_name);


来源:https://stackoverflow.com/questions/52276189/getting-the-date-as-per-the-format-in-which-month-name-is-coming-as-first

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!