Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Declaring a date

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Declaring a date

    Hi
    I'm new to Java and I have a project where I have to set a date (ex:Sep/19/1982) using String headers.
    I have to declare three reference variables that refer to String objects named month, day, and the year.
    for example:
    date = new String(“September 29, 2008”);
    or
    date = new String(“March 08, 2003”);
    or
    date = new String(“May 14, 1999”);
    Etc…
    it must work for multiple dates.


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Declaring a date

    Are you able to use the DateFormat class?

    If so, you can do some research on how to use DateFormat (I'm unsure) since the method DateFormat.getDateInstance(DateFormat.LONG) will give you exactly what you want.

    If you have to write this all your own without the use of JAVA established classes (which defeats the entire point of JAVA), you can have an array of Month names. Since you will provide the first 3 letters of the months, you can loop through the Month array and get the first three letters of each name by using the method:
    String s = String.substring(0,3);
    And find the correct month. From there, you can just return that index and presto, you have the full Month name.

    However, before you do that, you will have to separate the Month, Day, and Year. To do that, use the split method provided in the String class:
    String[] splitDate = String.split("/");
    With that method, you will know that splitDate[0] is the Month, splitDate[1] is the Day, and splitDate[2] is the Year.

    The last thing you have to do is set the day to have two digits. You can do that manually or find another Formatter. The manual way would be by determining if the Day value is 1 or 2 digits by using the method:
    int count = String.length();
    If it is 1, you know you need to add a 0 infront of it. Otherwise, leave it as it is.


    That should be pretty much everything. I'm sure you can do the rest. Keep in mind that I used the Anonymous Objects: String for the method examples above. You want to use those methods on actual Strings for them to work. If you have any trouble writing the code, just post what you have and I'll help you out.
    Last edited by aussiemcgr; October 11th, 2010 at 01:37 PM.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. The Following 2 Users Say Thank You to aussiemcgr For This Useful Post:

    Akim827 (October 11th, 2010), JavaPF (October 12th, 2010)

  4. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Declaring a date

    This is my setup:
    public class Proj2
    {
    public static void main(String []args)
    {
    String date, month, day, year;
    int dateLength;
    char lastNumber;
    date=new String("Dec 15, 1989");
    System.out.println("The date is"+" "+date);
    month=date.substring(0,3);
    System.out.println("The month is"+" "+month);
    day=date.substring(4,6);
    System.out.println("The day is"+" "+day);
    dateLength=date.length();
    lastNumber=date.charAt(dateLength -1);
    year=date.substring(8,dateLength -1);
    System.out.println("The year is"+" "+year+lastNumber);





    }
    }
    The problem is that if i change date=new String("November 19, 2001");
    the month variable will have the value "Nov" and it should have "November"
    the day variable will have the value "mb" and it should have "19"
    and the year variable will have the value " 19, 2001" and it should have "2001"
    I didn't learn the Split method, so I'm abit confused on how to approach that method
    Last edited by Akim827; October 11th, 2010 at 09:07 PM.

  5. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Declaring a date

    Ok, well the Split method is easy to use and will solve your issues. We first need to determine how the input will occur. If the input is formatted like this:
    November 19, 2001
    We know that each part is separated by a space. So, we can use the String.split(" "); method to split our date. The String.split(String); method requires us to send it a String as a delimiter. A delimiter is an indication as to how we want to split the String. The String.split(String) method returns an array of Strings, where each index holds a different part of the original String.
    So, the following code:
    //Test Case for Date
    date = "Dec 15, 1989";
    //Split the Date based on a space
    String[] splitDate = date.split(" ");
     
    //Based on the format of your date, you know the first index will be the month
    month = splitDate[0];
    /* This one is more complicated. Based on your format, you will have an extra comma
     * at the end of your day String. So, we need to use the substring method to remove
     * that comma. We are guarenteed it will be at the end, so we just need to remove
     * the last character.
     */
    day = splitDate[1];
    day = day.substring(0,day.length()-1);
    //Lastly, we can get the year String
    year = splitDate[2];
    will use the split method to separate your formatted string based on a space. We can then use those individual Strings to set our other variables.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  6. The Following User Says Thank You to aussiemcgr For This Useful Post:

    Akim827 (October 11th, 2010)

  7. #5
    Junior Member
    Join Date
    Oct 2010
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Declaring a date

    Wow thanks!!! You really helped me so much.

  8. #6
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Declaring a date

    Hello Akim827,

    If this thread is solved, please - http://www.javaprogrammingforums.com...ad-solved.html
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  9. #7
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Declaring a date

    @Aussiemcgr: instead of day = day.substring(0,day.length()-1); couldn't you just use day = splitDate[1].remove(",");

  10. #8
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Declaring a date

    Quote Originally Posted by Brt93yoda View Post
    @Aussiemcgr: instead of day = day.substring(0,day.length()-1); couldn't you just use day = splitDate[1].remove(",");
    Didn't even think of that. That would actually be much better. Or at least easier to understand.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. declaring a "final" variable?
    By needleman in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 26th, 2010, 07:55 PM
  2. <..> when declaring a class
    By Asido in forum Java Theory & Questions
    Replies: 1
    Last Post: September 8th, 2010, 08:47 AM
  3. Declaring variables in constructor and compiling
    By Newoor in forum Object Oriented Programming
    Replies: 3
    Last Post: December 5th, 2009, 01:07 PM