Link to home
Start Free TrialLog in
Avatar of rockyisin
rockyisin

asked on

Date Conversion: H

The following program using "SimpleDateFormat" forces the output to include the year "YYYY".  I dont want the year to be output.  I only want the month and day to be output.  What do I change to prevent the YYYY from being displayed and prevent it from being an option to be entered by the user.  

Thanks,

Rocky

import java.io.*;
import java.util.*;
import java.text.SimpleDateFormat;

public class DateChanger {
   public static void main (String[] args) throws IOException {

      // Title
      System.out.println("*******************");
      System.out.println("* Date Changer *");
      System.out.println("*******************");
      System.out.println();

      // initializes buffered input br
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

      // gets date parts and stores them as strings
      System.out.print(" Please enter a two digit month (MM): ");
      String monthstring = br.readLine();

      System.out.print("   Enter day (DD): ");
      String daystring   = br.readLine();

      System.out.print("Enter year (YYYY): ");
      String yearstring  = br.readLine();

      // converts date parts to ints
      int MM = Integer.parseInt(monthstring)-1; // account for GregorianCalendar weirdness
      int DD = Integer.parseInt(daystring  );
      int YY = Integer.parseInt(yearstring );

      // initializes simple date format sdf with appropriate time pattern strings
      SimpleDateFormat sdf = new SimpleDateFormat("MMMM dd, yyyy', which is a 'EEEE'.'");
      GregorianCalendar c1 = new GregorianCalendar(YY,MM,DD); // initializes GregorianCalender c1

      c1.set(YY,MM,DD); // sets c1 to inputed date
      Date unform_date = c1.getTime(); // gets time from c1, assigns it to unform_date
      String datestring = sdf.format(unform_date); // formats unform_date to string

      System.out.println("The date is " + datestring);

   } // end main
} // end DateChanger
Avatar of sudhakar_koundinya
sudhakar_koundinya

Change
 SimpleDateFormat sdf = new SimpleDateFormat("MMMM dd, yyyy', which is a 'EEEE'.'");

                      to

 SimpleDateFormat sdf = new SimpleDateFormat("MMMM dd', which is a 'EEEE'.'");


regards
Your code is working fine my side

By the way check your java/jvm versions also

here my side version is
java version "1.2.2"
Classic VM (build JDK-1.2.2_016, native threads, symcjit)
ASKER CERTIFIED SOLUTION
Avatar of sudhakar_koundinya
sudhakar_koundinya

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
Avatar of CEHJ
The following line is redundant as you have set the Calendar in the constructor:

>>c1.set(YY,MM,DD); // sets c1 to inputed date
yes, though it won't  give any problem, just unnecessary