Solved

Date Conversion:  H

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
nextBoolean(double p) for Random class 3 41
tomcat not starting 6 68
Which non-HTML GUI front end to use with Java? 3 24
hashmap order 17 37
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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 how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

860 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