Solved

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

Posted on 2004-09-15
6
292 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
  • 4
6 Comments
 
LVL 92

Expert Comment

by:objects
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:djchiena
Comment Utility
you are the man! Thanks alot for your help!!!
0
 

Expert Comment

by:Emilda
Comment Utility
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
Comment Utility
no worries :)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now