Solved

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

Posted on 2004-09-15
6
302 Views
Last Modified: 2012-05-05
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() + "," );
            }
            

      }
}
0
Comment
Question by:djchiena
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
6 Comments
 
LVL 92

Expert Comment

by:objects
ID: 12071285
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.

0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 12071293
you also need to only call nextToken() once, every time you call it parse anotyher token from string

String month = tokens.nextToken();
String day = tokens.nextToken();
String year = tokens.nextToken();
if (month.equals("01"))
{
   System.out.println("January");
}
else if (month.equals("02"))
{
   System.out.println("February");
}
etc.
0
 
LVL 92

Expert Comment

by:objects
ID: 12071298
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.
0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

Author Comment

by:djchiena
ID: 12071353
you are the man! Thanks alot for your help!!!
0
 

Expert Comment

by:Emilda
ID: 12071392
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);  

0
 
LVL 92

Expert Comment

by:objects
ID: 12071397
no worries :)
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question