Solved

Date Conversion:  H

Posted on 2003-11-06
5
612 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
[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
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ejb stateless example 2 66
ejb mdb examples 1 36
jsp CRUD operations with and without prepared statement also hibernatge 1 49
Java class and jar 3 58
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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…
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…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses

737 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