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
Solved

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

Posted on 2004-09-15
6
296 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
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

838 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