Solved

Date Conversion:  H

Posted on 2003-11-06
5
603 Views
Last Modified: 2012-08-13
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
0
Comment
Question by:rockyisin
  • 4
5 Comments
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 9699368
Change
 SimpleDateFormat sdf = new SimpleDateFormat("MMMM dd, yyyy', which is a 'EEEE'.'");

                      to

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


regards
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 9699383
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)
0
 
LVL 14

Accepted Solution

by:
sudhakar_koundinya earned 50 total points
ID: 9699387
Your code with slight modifications


    // 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', 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);
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9699678
The following line is redundant as you have set the Calendar in the constructor:

>>c1.set(YY,MM,DD); // sets c1 to inputed date
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 9699979
yes, though it won't  give any problem, just unnecessary
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
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 learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

744 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

12 Experts available now in Live!

Get 1:1 Help Now