Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Date Conversion:  H

Posted on 2003-11-06
5
Medium Priority
?
620 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 150 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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 regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

715 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