Link to home
Start Free TrialLog in
Avatar of djchiena
djchiena

asked on

Converting Dates in string format to readable date in JAVA using Tokens...PLEASE HELP!!!!

I have a problem trying to convert a date in mm/dd/yyyy format to a readable date.  I do not want to use the simpleDateFormat class.  I am able to split the mm/dd/yyyy format by delimiting it with a token but now I am having a problem trying to convert it to the readable date format.  If i coment out the large conditionals this is the result I get so far:

------------------------------------------------------------------------------------------------------------
Enter a date in mm/dd/yyyy format then press enter:
12/25/2004

Conversion of this date in readable format is:
12 25 2004
------------------------------------------------------------------------------------------------------------

I want to take the first token (the 12) and convert it to a string "December" and then print out the 25 and 2004 so it reads December 25, 2004.
Can anyone please help me debug my code!?!?!? thanks

import java.util.*;

public class dates
{
      public static void main( String args[] )
      {
            Scanner scanner = new Scanner( System.in );
            System.out.println( "Enter a date in mm/dd/yyyy format then press enter:" );
            String date = scanner.nextLine();

            StringTokenizer tokens = new StringTokenizer( date, "/" );
            
            
            System.out.println( "Conversion of this date in readable format is: " );

            while ( tokens.hasMoreTokens() )
            {
                  if( tokens.nextToken() == "01" )
                  {
                        System.out.printf( "January " );
                  }
                  if( tokens.nextToken() == "02" )
                  {
                        System.out.printf( "February " );
                  }
                  if( tokens.nextToken() == "03" )
                  {
                        System.out.printf( "March " );
                  }
                  if( tokens.nextToken() == "04" )
                  {
                        System.out.printf( "April " );
                  }
                  if( tokens.nextToken() == "05" )
                  {
                        System.out.printf( "May " );
                  }
                  if( tokens.nextToken() == "06" )
                  {
                        System.out.printf( "June " );
                  }
                  if( tokens.nextToken() == "07" )
                  {
                        System.out.printf( "July " );
                  }
                  if( tokens.nextToken() == "08" )
                  {
                        System.out.printf( "August " );
                  }
                  if( tokens.nextToken() == "09" )
                  {
                        System.out.printf( "September " );
                  }
                  if( tokens.nextToken() == "10" )
                  {
                        System.out.printf( "October " );
                  }
                  if( tokens.nextToken() == "11" )
                  {
                        System.out.printf( "November " );
                  }
                  if( tokens.nextToken() == "12" )
                  {
                        System.out.printf( "December " );
                  }

                  System.out.println( tokens.nextToken() + "," );
            }
            

      }
}
Avatar of Mick Barry
Mick Barry
Flag of Australia image

you need to check for string equality (using equals() method) instead of object equality

change:

  if( tokens.nextToken() == "01" )

to:

  if( tokens.nextToken().equals("01") )

same for the rest of the if's.

ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
might also be better to parse the string to an int eg.

int month = Integer.parseInt(tokens.nextToken());
if (month==1)
{
   ...
}
else if (month==2)
{
   etc.
Avatar of djchiena
djchiena

ASKER

you are the man! Thanks alot for your help!!!
Instead of using if-else statement you can use a String array to hold the names of month as well as shown below.

// First entry is given as empty as array index starts from 0.
String[] months = { "Empty", "January ", "February ", "March ", "April ", "May ", "June ",                                      "July ", "August ", "September ", "October ", "November ", "December "};

Then do as objects suggested,

String month = tokens.nextToken();
String day = tokens.nextToken();
String year = tokens.nextToken();
         
int index = Integer.decode(month).intValue();           // To convert to int value.
System.out.println( "Conversion of this date in readable format is: " );
                   
System.out.println( months[index] + day + "," + year);  

no worries :)