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:
How do I call the value of the date without the slashes? That seems to be my problem right now, I try to call the value of the string and it says I can't because of the slashes.
(Sorry, again, new to Java, just trying to get my sea legs.)
I have no idea what you're talking about.
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?
So you didn't understand my answer? Why are you asking how to get a certainly bad approach to work instead of saying that you didn't understood my answer, or to elaborate why you don't want to use the right way to handle the date?
You don't "get past" that. You should not have gone that way in the first place. Reply #1 was a perfectly good answer, why did you completely ignore it?
Well, I thought that's what I was attempting...to get the string value, that is, like you said to start with. There must be something I'm not seeing.
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);