Formatting date 2/24 as “February, 24”

放肆的年华 提交于 2019-12-25 03:44:41

问题


I am trying t make a date that comes in like this mm/dd turn into the name of the month and day like it comes in 8/15 i want it to say August, 15

public void printAlphabetical()
{
           int month,day;// i got the month and day from a user previously in my program

       String s = String.format("%B, %02d%n",month,day);
       Date date = new Date();
       date.parse(s);// this does not work
       System.out.printf(s);
}

回答1:


String[] months = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", 
                    "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}

System.out.println(months[month - 1] + ", " + day);



回答2:


    System.out.println( new SimpleDateFormat("MMMM, dd").format( 
                          new SimpleDateFormat("MM/dd").parse("2/24") ) );

mmhh dejavu? nahhh exact duplicate -> here




回答3:


See Calendar and SimpleDateFormat




回答4:


Take a look at the Java simple date format:

http://java.sun.com/j2se/1.4.2/docs/api/java/text/SimpleDateFormat.html




回答5:


You can do something like this,

            DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
            Date date = (Date)formatter.parse(dateStr + "/2000); // Must use leap year
            formatter = new SimpleDateFormat("MMMM, dd");
            System.out.println(formatter.format(date));



回答6:


2/24 as “February, 24”

So, the starting date has a pattern of M/d and the final date has a pattern of MMMM, d?

Use java.text.SimpleDateFormat wisely. First parse the String based on the desired pattern into a Date and then format the obtained Date into another String with the desired pattern.

Basic example:

String datestring1 = "2/24"; // or = month + "/" + day;
Date date = new SimpleDateFormat("M/d").parse(datestring1);
String datestring2 = new SimpleDateFormat("MMMM, d").format(date);
System.out.println(datestring2); // February, 24 (Month name is locale dependent!)


来源:https://stackoverflow.com/questions/1804000/formatting-date-2-24-as-february-24

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