This content has been marked as final. Show 12 replies
Get the String value, use the java.text.SimpleDateFormat to parse it to an java.util.Date object. Then get an instance of java.util.Calendar and set it with the Date object. Then add 5 days to it. Finally you can get the changed Date object back from the Calendar object and format it to String with SimpleDateFormat.
Here are the API documentations of interest:
Well, when I try to turn the string that has the original date in it into an integer, I can't because of the slashes.
Like, my first date is
I want to turn it into an integer so I can go dd+5 and make the end date at the end of the week.
But I can't turn it into an integer because of the / and / in the middle of it.
How do I get past this?
For the rest of the steps, please follow the links I gave in my first reply. The code almost writes itself if you follow my explanation carefully and consult the API documentation thoroughly.
String dateString = "03/01/08"; SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yy"); Date dateObject = simpleDateFormat.parse(dateString);
I can't have the string be a date set in the code though...I need to call it from a file, THEN use that called date to make a second string which has that date + 5 days.
I'm sorry to be such a bother, but I'm really just not grasping what it is I need to do...I've been staring at those documents for awhile now and everything I try doesn't work.
the777joker777 wrote:How does that make any difference? (Hint: It doesn't. A String is a String, regardless of whether it's a literal in your source code or read from a file or a DB or user input or generated randomly from the background radiation from the big bang. Literals are just easier to use here to demonstrate the concept.)
I can't have the string be a date set in the code though...I need to call it from a file,
Okay, I managed to figure out almost all of it. I'm almost there.
I tried to do this...
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yy");
Date date1 = simpleDateFormat.parse(info);
Calendar calendar = new GregorianCalendar();
String enddate1 = Integer.toString(calendar.MONTH);
String enddate2 = Integer.toString(calendar.DAY_OF_MONTH);
String enddate3 = Integer.toString(calendar.YEAR);
then at the end in the print section...
System.out.println("Memo: employee #" + num + " for the period " + info + " through " + enddate1 + "/" + enddate2 + "/" + enddate3);
But the enddates got returned as 2/5/1, which isn't accurate nor in proper format. I thought I declared the format at the top...
Get rid of all that rubbish with the enddate Strings.
A format is applied to a date when you want to print out a formatted date. So you have to actually make use of it. You also didn't read the documentation for Calendar clearly.Although again that's okay because you don't need it.
And then print the String newDateFormattedTheWayYouWan
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yy"); Date date1 = simpleDateFormat.parse(info); Calendar calendar = new GregorianCalendar(); calendar.setTime(date1); calendar.add(Calendar.DAY_OF_MONTH,5); Date date2 = calendar.getTime(); String newDateFormattedTheWayYouWant = simpleDateFormat.format(date2);