Best way to increment the date in a String URL? [duplicate] - java

This question already has answers here:
How to add days to a date in Java
(3 answers)
Closed 7 years ago.
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?

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"

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.

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)"

Related

Invalid ZoneDateTime parsing in Java / Scala [duplicate]

This question already has an answer here:
ZonedDateTime change behavior jdk 8/11
(1 answer)
Closed 2 years ago.
I am trying to parse dates from strings to ZonedDateTimes and I've come across a bizzare problem.
2020-11-01T01:00-05:00[America/New_York]
This is an hour right after time EDT ends this year. When I pass it to ZonedDateTime.parse I get
ZonedDateTime.parse("2020-11-01T01:00-05:00[America/New_York]")
// 2020-11-01T01:00-04:00[America/New_York]
but if I do
ZonedDateTime.parse("2020-11-01T01:00-04:00[America/New_York]").plusHours(1)
I get
2020-11-01T01:00-05:00[America/New_York]
So it's not like Java cannot represent this ambiguous value or something..
Can anyone explain to me that behavior and possible solution?
Note: I am using Java 8
As Amir Schnell said in the comments, this seems to be a bug in the JDK, as they cannot reproduce this in Java 11.
For now, I have found this work around:
Parse the string into a local date time, zone ID, and zone offset, and create a ZonedDateTime using those three things:
TemporalAccessor ta = DateTimeFormatter.ISO_ZONED_DATE_TIME.parse("2020-11-01T01:00-05:00[America/New_York]");
System.out.println(
ZonedDateTime.ofLocal(
LocalDateTime.from(ta),
ZoneId.from(ta),
ZoneOffset.from(ta)
)
);

get Date without time in java [duplicate]

This question already has answers here:
LocalDate to java.util.Date and vice versa simplest conversion? [duplicate]
(7 answers)
How do I get a Date without time in Java?
(23 answers)
Closed 3 years ago.
I want to get a Date without time, but always failed.
below is my codes:
long curLong = System.currentTimeMillis();
curLong = curLong - curLong % TimeUnit.DAYS.toMillis(1);
Date date = new Date(curLong);
System.out.println("date = " + date);
the output:
date = Mon Oct 28 08:00:00 CST 2019
anyone knows why? Thank you
It is not recommended to use java.util.Date anymore. It was called Date but doesn't necessarily hold only the date information but information about the time additionally.
Use this:
LocalDate today = LocalDate.now();
and print it as
System.out.println(today.format(DateTimeFormatter.ISO_DATE);
using the ISO date format. You can define your own formatting pattern using a
DateTimeFormatter.ofPattern("dd.MM.yyyy");
for example.
You can use java.time.LocalDate.now() to get just the date.
Anyway, your case doesn't work as you expect because you are doing nothing to remove the time from the date: you are just "repressing" it, that's why it's zero. If you want to continue this way you could always substring it (substring the Date.toString() of course I meant).
Hope I helped.
java.util.Date's javadoc states:
The class Date represents a specific instant in time, with millisecond precision.
Thats why you have date with time
If you want a date you can use : java.time.LocalDate.now() (Java 8+)
First of all, stop using the old java.util.Date. The new Java 8 date and time API has much better classes for all date and time operations.
The LocalDate class does exactly what you want.
The current date can be obtained by LocalDate.now().
It also has a lot of facilities to add and subtract days, months etc. and it takes into consideration all the calendar special cases for you.

Can I have more control on zone offset formatting at DateTimeFormatter [duplicate]

This question already has answers here:
SimpleDateFormat with TimeZone
(6 answers)
Closed 4 years ago.
Currently we are using
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSX")
To format the time range to an external vendor for a range of data, for India, that formatter will give time like this:
2018-04-26T00:00:00.000+0530
However, my vendor say they cannot accept this format and it have to look like
2018-04-26T00:00:00.000+05:30
However, look like in DateTimeFormatter, whatever I choose Z/z/X/x, I don't get that format of offset. Just wonder is that a way to customize the offset to be HH:mm?
Or, I need to get the offset in second and work that our myself?
It is three x. Just tried with JavaRepl:
java.time.format.DateTimeFormatter
.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSxxx")
.withZone(java.time.ZoneId.systemDefault())
.format(java.time.Instant.now())
Results in
java.lang.String res10 = "2018-04-27T11:06:50.648+00:00"
After some trial and error, I saw that this is also documented in the API documentation of DateTimeFormatter but it is not easy to find (buried in a lot of other text):
Three letters outputs the hour and minute, with a colon, such as '+01:30'
DateTimeFormatter API Documentation

Elegant way to convert "YYYY-MM" to Date object in Java [duplicate]

This question already has answers here:
Why my pattern("yyyyMM") cannot parse with DateTimeFormatter (java 8)
(2 answers)
Closed 5 years ago.
What is the best way to convert date in String format "YYYY-MM" to Date object in Java apart from String parsing.
What I tried is below.
String expirationDateArr[] = dateStr.split("-");
Then extract month and year to create the Date object.
Don't use Date. Since Java 8 we have time package which includes YearMonth class. With it your code can be as simple as
YearMonth ym = YearMonth.parse("2017-10");

From YearMonth(yyyy-MM) to YearMonth(yyyy-MMMM) [duplicate]

This question already has an answer here:
Generate & parse "Year-Month" values in text from Java
(1 answer)
Closed 6 years ago.
I have a variable:
YearMonth date;
where inside standing "2016-07", for example.
I want it to be still YearMonth, but with "2016 july"(note , there's no "-" separator), or, even better, "2016 luglio", that is italian Locale.
How to do it?
UPDATE
I tried with JackDaniels's method. It could actually work, as it gives me a string(str) with the right format of date. But I need to put that string again into my YearMonth variable. I tried with:
myVariable = YearMonth.parse(str);
but it returns me an error: java.time.format.DateTimeParseException: Text '2014 gennaio' could not be parsed at index 4
How do I solve this?
Use the new DateTimeFormatter to parse and format YearMonth directly and use Locale to set the locale you need.
Don't use SimpleDateFormat. That is part of the old Date API.
Try running the code to see if this is what you want in Codiva online compiler IDE.
YearMonth yearMonth = YearMonth.of(2016, 7);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy MMMM",
new Locale("it", "IT"));
System.out.println(yearMonth.format(formatter));
Full working code in Codiva.

Categories

Resources