Time as a string parser in java [duplicate] - java

This question already has answers here:
Parsing time strings like "1h 30min"
(8 answers)
Closed 6 years ago.
Is there any libraries that is able to take a string such as 5d 1h 2m 15s and add it to a java date / java Calendar?
ie a system property will be set as 5d 1h 2m 15s
We will read in the system property and add this amount of time to the current date.
Otherwise I will have to implement this as a long in milliseconds.

you'll need a bit of parsing to extract the component element but Joda-Time's duration should help you out
http://joda-time.sourceforge.net/apidocs/org/joda/time/Duration.html
If you are using Java8 joda-time's concept were integrated so no need to external dependencies
https://docs.oracle.com/javase/8/docs/api/java/time/Duration.html
once you have a duration adding it to the current date time should be trivial
Java8 and jodaTime
Instant ajustedTime = Instant.now().plus(yourDurationInstance);
you can convert to and from Java's date pretty easily, others have already answered here

Related

Offset not calculated correctly at OffsetDateTime to Instant conversion in Java 8 [duplicate]

This question already has answers here:
ZonedDateTime America/Phoenix zone to GMT having issue [duplicate]
(3 answers)
Java Date Time conversion to given timezone
(3 answers)
Closed 7 months ago.
I would need the community's help because I could not find the answer in the Java documentation. I don't understand how the offset is taken into the math calculations when I try to convert an OffsetDateTime (ex: 2022-07-09T11:30:34) object to an Instant object. For example:
If we would run on OpenJDK 1.8 the command in a main function: OffsetDateTime.parse("2022-07-09T12:30:34+01:00", DateTimeFormatter.ISO_OFFSET_DATE_TIME).toInstant() the outcome would be an Instance of date-and-time 2022-07-09T11:30:34 when I would had expected an Instant of 2022-07-09T13:30:34. The difference is the hour. Why do I get it like this?
And the opposite using -01:00 will do the revet.
I apologize for not formatting my text correctly or if I missed something. I would appreciate it if my post would not be marked us not worthy. And sorry if the answer was already answered in a different thread, which I could not find.
Thank you in advance.

how do you account for two possible date formats one with milliseconds and one without when parsing date using simpledateformat? [duplicate]

This question already has answers here:
How to parse dates in multiple formats using SimpleDateFormat
(13 answers)
Java LocalDateTime.parse with millisecond precision but optional microsecond precision
(1 answer)
How to check validity of Date String?
(4 answers)
Closed 2 years ago.
There can be two different date time formats as shown below. The second variant has milliseconds time.
2020-09-07T16:15:42Z
2020-09-09T11:41:58.5152Z
Currently i am using this way to parse the date. Is there a way to specify a single format to account for both of these two cases?
def dateutc = (new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSS'Z'"))
dateutc.setTimeZone(TimeZone.getTimeZone("UTC"))
time = dateutc.parse(point.time.toString()).getTime()
Thanks for the help!
Please note i am using java 7.

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

Java 8 class to represent time intervals [duplicate]

This question already has answers here:
Is there a class in java.time comparable to the Joda-Time Interval?
(4 answers)
Closed 6 years ago.
Java 8 introduced a new Time & Date API, with classes like Period or Duration.
Now I'm looking for a class to represent date intervals, e.g. "from 4th August 2016 to 8th August 2016" and answer the question: do these intervals overlap. Period doesn't seem to satisfy this, since it works in an affine way, not knowing where the interval has started, only how long it takes.
Is there any Java 8 standard library class to suit my needs? Or do I have to write my own?
You could resolve the date down into a long and use an IntervalTree. Here's one I made earlier.

How to guess unknown dateTime format? [duplicate]

This question already has answers here:
How to get date datatype from sql database to java?
(2 answers)
Closed 7 years ago.
I have a DateTime which I received as JSON via a REST Servce of the Couchbase's Sync Gateway:
"2015-05-20T13:32:25.9999478-07:00"
I do not have the access to the Sync Gateway's configs.
I did no find any documentation about the default format of Sync Gateway's dateTime format.
I do not understand what the .9999478-07:00" means.
Is there a way to guess that somehow?
That is the ISO standard notation for date time
YYYY-MM-DD,
then a 'T' for time, HH:MI:SS.S* (fractional seconds),
+/- time zone (there are half our zones!)
Time and also time zone optional.
Look in the wikipedia or javadoc.
When no time zone the date time representation can be sorted alphabetical to be naturally ordered.

Categories

Resources