-1

So I have a URL that contains date=2016-01-25 somewhere in between. My goal is, when a user enters the URL and n days (in a GUI application's text fields), it should go into a loop of n times and increment the date from the URL. That also means it goes to the next month if it is 30/31. Anyone have an optimized approach to this?

btrballin
  • 1,420
  • 3
  • 25
  • 41
  • Your Question is not clear. Are you asking how to increment a date day-after-day for a certain number of days? What does that have to do with URLs? The key to a successful Question on StackOverflow is to be narrowly focused. Please edit your Question to clarify and strip away the extraneous details. – Basil Bourque Jan 25 '16 at 23:38
  • You can forget about the fact that it is a URL. All I need is the URL in String format to increment the date for the specified number of days because the URL executes a certain javascript function for that date within the URL. So if I can loop through the same URL and just increment the dates, I can execute the javascript function for each day rather than manually clicking "next" and then clicking on a button in the web page that executes the function. – btrballin Jan 26 '16 at 00:41
  • Also duplicate of [Java string to date conversion](http://stackoverflow.com/q/4216745/642706) and many others. – Basil Bourque Jan 26 '16 at 01:06

3 Answers3

1

Assuming you are using Java 8, you can use the java.time.LocalDate class to parse, add days, and convert back to a string. Here's an example:

String date = "2016-01-25";
LocalDate localDate = LocalDate.parse(date);
localDate = localDate.plusDays(15);
System.out.println(localDate); // Prints "2016-02-09"
1

To add to the Java 8 solutions, if you're using an earlier version of Java you can use JodaTime's LocalDate class to accomplish the same thing. Syntax will be the same as in Java 8.

LAROmega
  • 488
  • 4
  • 13
0

You would need to parse the date string out of the URL, using something like regex and then create a Date object increment your date and rebuild the URL.

Java's Date primitives such as LocalDate have support for "plusDays(int n)"

Hamel Kothari
  • 717
  • 4
  • 11