0

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);
}
Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
AlThePal78
  • 793
  • 2
  • 14
  • 25

6 Answers6

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

mmhh dejavu? nahhh exact duplicate -> here

Community
  • 1
  • 1
OscarRyz
  • 196,001
  • 113
  • 385
  • 569
1

See Calendar and SimpleDateFormat

Maxime ARNSTAMM
  • 5,274
  • 10
  • 53
  • 76
1

Take a look at the Java simple date format:

https://docs.oracle.com/javase/1.5.0/docs/api/java/text/SimpleDateFormat.html

fospathi
  • 537
  • 1
  • 6
  • 7
Karl
  • 2,927
  • 8
  • 31
  • 39
1

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));
ZZ Coder
  • 74,484
  • 29
  • 137
  • 169
0
String[] months = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", 
                    "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}

System.out.println(months[month - 1] + ", " + day);
Amarghosh
  • 58,710
  • 11
  • 92
  • 121
0

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!)
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • What about linking directly to http://stackoverflow.com/questions/1800091/java-date-format-real-simple/1800202#1800202? – Pascal Thivent Nov 26 '09 at 14:56
  • That's just a compilation error. The posted code compiles fine. Maybe you imported `java.sql.Date` instead of `java.util.Date`. That's often the first class with which an IDE pops up and also often which causes problems among the unawareness. – BalusC Nov 26 '09 at 15:44
  • @Pascal: although the concepts are the same, but that's a different answer. – BalusC Nov 26 '09 at 15:46