# Integer Dates to String Dates Problem

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 08-20-2013, 05:40 AM
HelpingIsCaring
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.
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]);         }```
• 08-20-2013, 07:26 AM
DarrylBurke
Re: Integer Dates to String Dates Problem
Homework? If it's not, just use SimpleDateFormat.

db
• 08-20-2013, 07:17 PM
HelpingIsCaring
Re: Integer Dates to String Dates Problem
Quote:

Originally Posted by DarrylBurke
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.
• 08-20-2013, 07:45 PM
sehudson
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?
• 08-20-2013, 07:49 PM
HelpingIsCaring
Re: Integer Dates to String Dates Problem
Quote:

Originally Posted by sehudson
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.
• 08-20-2013, 07:54 PM
sehudson
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.
• 08-20-2013, 08:00 PM
HelpingIsCaring
Re: Integer Dates to String Dates Problem
Quote:

Originally Posted by sehudson
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?
• 08-20-2013, 08:14 PM
sehudson
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:
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:
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:

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?
• 08-20-2013, 08:29 PM
HelpingIsCaring
Re: Integer Dates to String Dates Problem
everything is in one loop right???
• 08-20-2013, 08:33 PM
sehudson
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.
• 08-20-2013, 08:46 PM
HelpingIsCaring
Re: Integer Dates to String Dates Problem
why wont it compile???
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]);         }```
• 08-20-2013, 08:54 PM
HelpingIsCaring
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 :)
• 08-20-2013, 09:05 PM
sehudson
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.

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]);         }   } }```
• 08-20-2013, 09:18 PM
HelpingIsCaring
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??
• 08-20-2013, 09:19 PM
sehudson
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.
• 08-20-2013, 09:22 PM
HelpingIsCaring
Re: Integer Dates to String Dates Problem
well here it is!
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.
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)```
• 08-20-2013, 09:25 PM
sehudson
Re: Integer Dates to String Dates Problem
We're going in circles. Use this code:
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]);         }   } }```
• 08-20-2013, 09:29 PM
HelpingIsCaring
Re: Integer Dates to String Dates Problem
cool. it compiles, but all the dates are wrong and why is this a slash on date
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
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)```
• 08-20-2013, 09:33 PM
sehudson
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

Code:

``` for (int i = 0; i < dataValue.length; i+=3) { }```
• 08-20-2013, 09:43 PM
HelpingIsCaring
Re: Integer Dates to String Dates Problem
Quote:

Originally Posted by sehudson
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.
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last