Solved

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

Posted on 2004-09-15
6
298 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to get all the API from website? 11 161
numbers ascending pyramid 101 265
login form jsp example 2 71
Is there a simpler dropbox system? 10 53
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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

751 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