Results 1 to 3 of 3
  1. #1
    gabrielpr12 is offline Member
    Join Date
    Nov 2011
    Posts
    41
    Rep Power
    0

    Default stuck on writing Date application

    I have to write an application to convert a date entered from the keyboard in the format 5/06/2000 to May 5, 2000 and then display the day of the year. I have to use arrays to store the names of the months as well as the days in each month. This is the code I have so far, but I don't know how proceed after the user inputs the date. How do I begin converting the input date from one format to another?

    import java.util.Scanner;
    import java.util.*;

    public class ConvertDate
    {
    public static void main(String[] args)
    {
    String aDate;
    String[] month = {"January", "February", "March", "April", "May", "June", "July", "August",
    "September", "October", "November", "December"};
    int[] numberOfDays = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    Scanner input = new Scanner(System.in);
    System.out.println("Please enter a date using the MM/DD/YYYY format:");
    aDate = input.nextLine();
    //need to find how to convert the input to the Month name
    //need to find a way to use the Gregorian Calendar to
    //convert the input day and month to the day of the year
    }
    }

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default Re: stuck on writing Date application

    SimpleDateFormat (Java Platform SE 6)
    GregorianCalendar (Java Platform SE 6)

    If you can use other classes, you could use the SimpleDateFormatter with the pattern "MM/dd/yyyy" and the method parse to parse the aDate-string to an date object (otherwise: you could use the split method of the string class and Integer.parseInt)

    //need to find a way to use the Gregorian Calendar to
    create a gregoriancalendar object and setTime and pass the date object

    //need to find how to convert the input to the Month name
    month[gregoriancalendarobject.get(Calendar.MONTH)] , see API-doc

    //convert the input day and month to the day of the year
    gregoriancalendarobject.get(Calendar.DAY_OF_YEAR) , see API-doc

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,387
    Rep Power
    25

    Default Re: stuck on writing Date application

    See the Calendar and DateFormat classes. They have methods that will help you.

Similar Threads

  1. Replies: 5
    Last Post: 02-16-2012, 09:26 AM
  2. Replies: 2
    Last Post: 05-12-2011, 04:52 PM
  3. Replies: 3
    Last Post: 04-05-2010, 11:02 PM
  4. Date application - help please!
    By javanewbie12 in forum New To Java
    Replies: 6
    Last Post: 12-08-2009, 02:07 AM
  5. Creating a Gregorian Calendar using a Date object gives date - 1
    By prachi_goliwadekar in forum New To Java
    Replies: 1
    Last Post: 05-08-2008, 08:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •