views:

143

answers:

6

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);
}
A: 
String[] months = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", 
                    "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}

System.out.println(months[month - 1] + ", " + day);
Amarghosh
He want full month names and you're leaving the SimpleDateFormat capabilities aside.
BalusC
I don't mind him expanding `Feb` to `February`.
Amarghosh
http://java.sun.com/j2se/1.4.2/docs/api/java/text/DateFormatSymbols.html
Alberto Zaccagni
+1  A: 

See Calendar and SimpleDateFormat

Maxime ARNSTAMM
+1  A: 

Take a look at the Java simple date format:

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

Karl
A: 

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
What about linking directly to http://stackoverflow.com/questions/1800091/java-date-format-real-simple/1800202#1800202?
Pascal Thivent
I tried this it said incompatibal types as an error
daddycardona
I tried this and i get an error incompatible type
daddycardona
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
@Pascal: although the concepts are the same, but that's a different answer.
BalusC
+1  A: 

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
i tried this got inconnvertible types
daddycardona
Sorry. I have the month and day reversed. Edited.
ZZ Coder
+2  A: 
    System.out.println( new SimpleDateFormat("MMMM, dd").format( 
                          new SimpleDateFormat("MM/dd").parse("2/24") ) );

mmhh dejavu? nahhh exact duplicate -> here

OscarRyz
As I interpret it, he rather wanted `d` instead of `dd`.
BalusC
Yeah, it is a mystery. It works the same anyway. Parsing 2/24 yields february, 24.
OscarRyz
Yes, but formatting using `d` yields February, 1 and formatting using `dd` yields February, 01.
BalusC