Page 1 of 2 12 LastLast
Results 1 to 20 of 33
Like Tree1Likes

Thread: Integer Dates to String Dates Problem

  1. #1
    Join Date
    Jun 2013
    Posts
    71
    Rep Power
    0

    Default Integer Dates to String Dates Problem

    i dont really know what to put in the for loops so that it goes through the dataValue by sets of three and then replaces the month and day values with string values.
    Java Code:
    public static void main(String[] args) {
            // TODO code application logic here
            int x;
            int[] dataValue = {28, 3, 1997, 20, 2, 2012, 16, 12, 2011, 9, 10, 1867};
            String[] month = {"January, February, March, April, May, June, July, Augest,Septemeber, October, November, December"};
            String[] singleDate = {"1st, 2nd, 3rd, 4th, 5th, 6th, 7th, 8th, 9th, 10th, 11th, 12th, 13th, 14th, 15th, 16th, 17th, 18th, 19th, 20th, 21st, 22nd, 23rd, 24th, 25th, 26th, 27th, 28th, 29th, 30th, 31st"};
            for (int i = 0; i < dataValue.length; i = i + 3) {
                System.out.println(dataValue[i] + "," + dataValue[i + 1] + "," + dataValue[i + 2]);
            }
    
            for (x = 0; x < month.length; x++) {
                
                //System.out.println(month[x]);
            }
            for (x = 0; x < singleDate.length; x++) {
                //System.out.println(singleDate[x]);
            }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,422
    Rep Power
    20

    Default Re: Integer Dates to String Dates Problem

    Homework? If it's not, just use SimpleDateFormat.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Join Date
    Jun 2013
    Posts
    71
    Rep Power
    0

    Default Re: Integer Dates to String Dates Problem

    Quote Originally Posted by DarrylBurke View Post
    Homework? If it's not, just use SimpleDateFormat.

    db
    it says i should use for loops, so im just trying to figure out this thing using for loops. but i could try out SimpleDateFormat. whatever gets the job done faster and simpler xD BUT I NEED TO USE MY INT ARRAY OF INT DATES.
    Last edited by HelpingIsCaring; 08-20-2013 at 08:37 PM.

  4. #4
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default Re: Integer Dates to String Dates Problem

    I tried to give you some help with this yesterday, I thought.
    The first loop that you have here will print:

    28, 3, 1997
    20, 2, 2012
    16, 12, 2011
    9, 10, 1867

    The SimpleDateFormat, as Darryl said, is your best bet. You may have to do 2 passes to get the "st, nd, rd, th" suffix on the day of the month, I don't think there is a mask that will add that for you.

    So I would assign the year, month, and day values to variables (inside your for loop). Then, use the SimpleDateFormat to get the Month and Day as a partial Date String(for example, you would end up with 'March 28' for the 1st date. Then, pass the day (28) to a method that will return the appropriate suffix. Once you have that, just append that suffix to the 'March 28' string, and then append the year, and you end up with 'March 28th 1997'.

    Make sense?
    Last edited by sehudson; 08-20-2013 at 08:53 PM.

  5. #5
    Join Date
    Jun 2013
    Posts
    71
    Rep Power
    0

    Default Re: Integer Dates to String Dates Problem

    Quote Originally Posted by sehudson View Post
    I tried to give you some help with this yesterday, I thought.
    The first loop that you have here will print:

    28, 3, 1997
    20, 2, 2012
    16, 12, 2011
    9, 10, 1867

    As Darryl said, you can just use SimpleDateFormat to create a String representation of those values in the format that you want.
    the question is how do i still use my array of int dates and also use SimpleDateFormat to convert them to string dates.

  6. #6
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default Re: Integer Dates to String Dates Problem

    Look at my updated answer, you may have read it while I was in the middle of editing it. See if that makes more sense.

  7. #7
    Join Date
    Jun 2013
    Posts
    71
    Rep Power
    0

    Default Re: Integer Dates to String Dates Problem

    Quote Originally Posted by sehudson View Post
    I tried to give you some help with this yesterday, I thought.
    The first loop that you have here will print:

    28, 3, 1997
    20, 2, 2012
    16, 12, 2011
    9, 10, 1867

    The SimpleDateFormat, as Darryl said, is your best bet. You may have to do 2 passes to get the "st, nd, rd, th" suffix on the day of the month, I don't think there is a mask that will add that for you.

    So I would assign the year, month, and day values to variables (inside your for loop). Then, use the SimpleDateFormat to get the Month and Day as a partial Date String(for example, you would end up with 'March 28' for the 1st date. Then, pass the day (28) to a method that will return the appropriate suffix. Once you have that, just append that suffix to the 'March 28' string, and then append the year, and you end up with 'March 28th 1997'.

    Make sense?
    i get the overall idea. Also, when u say assign month to a variable, u mean assign the old array to a new array?
    Last edited by HelpingIsCaring; 08-20-2013 at 09:12 PM.

  8. #8
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default Re: Integer Dates to String Dates Problem

    Here is the easiest way I can give you:

    -Inside of your loop, initialize 3 int values to hold the month, day, and year.
    For example:
    Java Code:
    int day = dataValue[i];
    int month = dataValue[i+1];
    int year = dataValue[i+2];

    -Create a Date object using those 3 int values.
    For example:
    Java Code:
    Date asDate = new Date(year, month, day);
    -Use SimpleDateFormat to create a String formatted as the full month spelled out, along with the day (i.e. March 28).
    Here is an example of using the SimpleDateFormat:

    Java Code:
     
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMMMM dd");
    String formattedDate = simpleDateFormat.format(date);
    //Now, you have a String called formattedDate that would look something like "March 28".
    The last thing you need to do is write a method (or find one online, there are plenty out there) that will give you the suffix that is appropriate for the day of the month passed in. So if you pass 28 to the method, it would return "th", if you pass it 2, it would return "nd", etc.

    Let's say you call that method getSuffix, The end result String that you want will be:

    formattedDate +getSuffix(day)+" "+year;

    Got it?
    Last edited by sehudson; 08-20-2013 at 09:18 PM.

  9. #9
    Join Date
    Jun 2013
    Posts
    71
    Rep Power
    0

    Default Re: Integer Dates to String Dates Problem

    everything is in one loop right???

  10. #10
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default Re: Integer Dates to String Dates Problem

    Yes, you can do that in 1 loop.
    I know you said that your goal was to write the Strings to a file, so I would declare an ArrayList of Strings above the loop, and as you go through the loop, add the formatted date Strings to that ArrayList.
    Then, once the loop is finished, perform the writing to the file. That way you aren't opening/closing the file multiple times, you can do it all at one time.

  11. #11
    Join Date
    Jun 2013
    Posts
    71
    Rep Power
    0

    Default Re: Integer Dates to String Dates Problem

    why wont it compile???
    Java Code:
    public class Date {
    
        public Date(){
        }
        public Date(int a , int b, int c) {
        }
        
        public static void main(String[] args) {
            // TODO code application logic here
            int[] dataValue = {28, 3, 1997, 20, 2, 2012, 16, 12, 2011, 9, 10, 1867};
            //String[] month = {"January, February, March, April, May, June, July, Augest,Septemeber, October, November, December"};
            //String[] singleDate = {"1st, 2nd, 3rd, 4th, 5th, 6th, 7th, 8th, 9th, 10th, 11th, 12th, 13th, 14th, 15th, 16th, 17th, 18th, 19th, 20th, 21st, 22nd, 23rd, 24th, 25th, 26th, 27th, 28th, 29th, 30th, 31st"};
            for (int i = 0; i < dataValue.length; i++) {
                int day = dataValue[i];
                int month = dataValue[i+1];
                int year = dataValue[i+2];
                Date date = new Date(year, month, day);
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMMMM dd");
                String formattedDate = simpleDateFormat.format(date);
                System.out.println(formattedDate);
                //System.out.println(dataValue[i] + "," + dataValue[i + 1] + "," + dataValue[i + 2]);
            }
    Last edited by HelpingIsCaring; 08-20-2013 at 09:50 PM.

  12. #12
    Join Date
    Jun 2013
    Posts
    71
    Rep Power
    0

    Default Re: Integer Dates to String Dates Problem

    thanks for all of your help bro! i hope u can hang in here and help me finish this! ill give you a like or something :)

  13. #13
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default Re: Integer Dates to String Dates Problem

    You may be confusing yourself by calling the Class that your main is inside of Date. The compiler thinks you are trying to create one of YOUR Date objects, not a java.util.Date object. Since there is no SimpleDateFormat method called format that takes in an instance of YOUR Date, it won't compile (it wants the java.util.Date)

    Rename your class to something like DateFileWriter(?) and make sure you have the java.util.Date imported.

    Java Code:
    public class DateFileWriter{
          
        public static void main(String[] args) {
            // TODO code application logic here
            int[] dataValue = {28, 3, 1997, 20, 2, 2012, 16, 12, 2011, 9, 10, 1867};
            //String[] month = {"January, February, March, April, May, June, July, Augest,Septemeber, October, November, December"};
            //String[] singleDate = {"1st, 2nd, 3rd, 4th, 5th, 6th, 7th, 8th, 9th, 10th, 11th, 12th, 13th, 14th, 15th, 16th, 17th, 18th, 19th, 20th, 21st, 22nd, 23rd, 24th, 25th, 26th, 27th, 28th, 29th, 30th, 31st"};
            for (int i = 0; i < dataValue.length; i++) {
                int day = dataValue[i];
                int month = dataValue[i+1];
                int year = dataValue[i+2];
                Date date = new Date(year, month, day);
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMMMM dd");
                String formattedDate = simpleDateFormat.format(date);
                System.out.println(formattedDate);
                //System.out.println(dataValue[i] + "," + dataValue[i + 1] + "," + dataValue[i + 2]);
            }
       }
    }
    Last edited by sehudson; 08-20-2013 at 10:08 PM.

  14. #14
    Join Date
    Jun 2013
    Posts
    71
    Rep Power
    0

    Default Re: Integer Dates to String Dates Problem

    i changed the name of the class and imported java.util.Date, so now there should be a method called format and everything should compile now!!! but it still doesn't??

  15. #15
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default Re: Integer Dates to String Dates Problem

    Post your code again. The format method is a part of the SimpleDateFormat class, don't worry about that part.

  16. #16
    Join Date
    Jun 2013
    Posts
    71
    Rep Power
    0

    Default Re: Integer Dates to String Dates Problem

    well here it is!
    Java Code:
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    public class DateFileWriter {
    
        public DateFileWriter(){
        }
        public DateFileWriter(int a , int b, int c) {
        }
        
        public static void main(String[] args) {
            // TODO code application logic here
            int[] dataValue = {28, 3, 1997, 20, 2, 2012, 16, 12, 2011, 9, 10, 1867};
            //String[] month = {"January, February, March, April, May, June, July, Augest,Septemeber, October, November, December"};
            //String[] singleDate = {"1st, 2nd, 3rd, 4th, 5th, 6th, 7th, 8th, 9th, 10th, 11th, 12th, 13th, 14th, 15th, 16th, 17th, 18th, 19th, 20th, 21st, 22nd, 23rd, 24th, 25th, 26th, 27th, 28th, 29th, 30th, 31st"};
            for (int i = 0; i < dataValue.length; i++) {
                int day = dataValue[i];
                int month = dataValue[i+1];
                int year = dataValue[i+2];
                DateFileWriter date = new DateFileWriter(year, month, day);
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMMMM dd");
                String formattedDate = simpleDateFormat.format(date);
                System.out.println(formattedDate);
                //System.out.println(dataValue[i] + "," + dataValue[i + 1] + "," + dataValue[i + 2]);
            }
    this is the exception.
    Java Code:
    Exception in thread "main" java.lang.IllegalArgumentException: Cannot format given Object as a Date
    	at java.text.DateFormat.format(DateFormat.java:301)
    	at java.text.Format.format(Format.java:157)
    	at date.DateFileWriter.main(DateFileWriter.java:32)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 0 seconds)
    Last edited by HelpingIsCaring; 08-20-2013 at 10:24 PM.

  17. #17
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default Re: Integer Dates to String Dates Problem

    We're going in circles. Use this code:
    Java Code:
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    public class DateFileWriter{
           
        public static void main(String[] args) {
            // TODO code application logic here
            int[] dataValue = {28, 3, 1997, 20, 2, 2012, 16, 12, 2011, 9, 10, 1867};
            //String[] month = {"January, February, March, April, May, June, July, Augest,Septemeber, October, November, December"};
            //String[] singleDate = {"1st, 2nd, 3rd, 4th, 5th, 6th, 7th, 8th, 9th, 10th, 11th, 12th, 13th, 14th, 15th, 16th, 17th, 18th, 19th, 20th, 21st, 22nd, 23rd, 24th, 25th, 26th, 27th, 28th, 29th, 30th, 31st"};
            for (int i = 0; i < dataValue.length; i++) {
                int day = dataValue[i];
                int month = dataValue[i+1];
                int year = dataValue[i+2];
                Date date = new Date(year, month, day);
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMMMM dd");
                String formattedDate = simpleDateFormat.format(date);
                System.out.println(formattedDate);
                //System.out.println(dataValue[i] + "," + dataValue[i + 1] + "," + dataValue[i + 2]);
            }
       }
    }
    Last edited by sehudson; 08-20-2013 at 10:28 PM.

  18. #18
    Join Date
    Jun 2013
    Posts
    71
    Rep Power
    0

    Default Re: Integer Dates to String Dates Problem

    cool. it compiles, but all the dates are wrong and why is this a slash on date
    Java Code:
    import java.text.SimpleDateFormat;
    import java.util.Date;
     
    public class DateFileWriter{
            
        public static void main(String[] args) {
            // TODO code application logic here
            int[] dataValue = {28, 3, 1997, 20, 2, 2012, 16, 12, 2011, 9, 10, 1867};
            //String[] month = {"January, February, March, April, May, June, July, Augest,Septemeber, October, November, December"};
            //String[] singleDate = {"1st, 2nd, 3rd, 4th, 5th, 6th, 7th, 8th, 9th, 10th, 11th, 12th, 13th, 14th, 15th, 16th, 17th, 18th, 19th, 20th, 21st, 22nd, 23rd, 24th, 25th, 26th, 27th, 28th, 29th, 30th, 31st"};
            for (int i = 0; i < dataValue.length; i++) {
                int day = dataValue[i];
                int month = dataValue[i+1];
                int year = dataValue[i+2];
                Date date = new /*Date*/(year, month, day); //the commented out word
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMMMM dd");
                String formattedDate = simpleDateFormat.format(date);
                System.out.println(formattedDate);
                //System.out.println(dataValue[i] + "," + dataValue[i + 1] + "," + dataValue[i + 2]);
            }
    well it doesnt show here, but there is a slash
    this is the output
    Java Code:
    April 28
    June 03
    February 17
    March 20
    September 02
    November 02
    January 16
    August 12
    April 02
    November 09
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 12
    	at date.DateFileWriter.main(DateFileWriter.java:21)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 0 seconds)
    Last edited by HelpingIsCaring; 08-20-2013 at 10:35 PM.

  19. #19
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default Re: Integer Dates to String Dates Problem

    The constructor that you're using for Date has been deprecated(superseded), but for your purposes, it should be just fine. I overlooked the for loop. The i should be incremented by 3 each time

    Java Code:
     for (int i = 0; i < dataValue.length; i+=3) {
    
    }
    Last edited by sehudson; 08-20-2013 at 10:49 PM.

  20. #20
    Join Date
    Jun 2013
    Posts
    71
    Rep Power
    0

    Default Re: Integer Dates to String Dates Problem

    Quote Originally Posted by sehudson View Post
    The constructor that you're using for Date has been deprecated(superseded), but for your purposes, it should be just fine. At this point, all you need to do is find the method that will give you your suffix, and combine the formattedDate, the suffix, and the year, and that should give you the date in "March 28th 2012" format.
    so they are suppose to be wrong??? can u tell me why that is??? oh i get it its the index thing. its one more month than the actual month. or one less.
    Last edited by HelpingIsCaring; 08-20-2013 at 10:47 PM.

Page 1 of 2 12 LastLast

Similar Threads

  1. Holiday Dates HELP PLEASE!
    By rampage12345 in forum New To Java
    Replies: 3
    Last Post: 10-21-2012, 07:42 AM
  2. Dates sometimes off by a day?
    By saggio in forum Advanced Java
    Replies: 13
    Last Post: 12-08-2010, 05:56 PM
  3. UTC Dates
    By PedroCosta in forum Advanced Java
    Replies: 3
    Last Post: 04-01-2010, 07:08 PM
  4. missing dates problem in ireport
    By javastuden in forum Advanced Java
    Replies: 0
    Last Post: 12-18-2009, 06:11 AM
  5. Help with dates
    By bumblyb33 in forum New To Java
    Replies: 1
    Last Post: 02-13-2009, 03:54 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
  •