• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Creating Temporal from time and timezone

 
Ranch Hand
Posts: 595
6
jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have a requirement where I have time in format “hh:mm” and timezone. I have to create time such that it can be used everyday.

I tried using Instant but it doesnt take this format. I tried using LocalTime but not able to use it properly.

Using LocalDateTime also did not work.

I want to get some temporal object which can be used with
What would be the optimal way to do it?
 
Saloon Keeper
Posts: 15485
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to explain your requirement a bit more. What do you mean by a "time that can be used every day"?

Time zones only make sense when you have a known place and an exact moment in time. They don't work for local times, and they don't work for times on unknown dates.

Between what things do you need to get the duration?
 
s ravi chandran
Ranch Hand
Posts: 595
6
jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the response.

Here is the scenario:
I set some time when my application should start. I take two parameter from user like start time and timezone. I need to create date/ temporal object from this so that everyday application starts i can use this object to compare if current time matches configured start time. I will trigger some action when they match. Here I can either compare it or find the duration between current time and configured time and create a delay to trigger action after this duration.

This check will happen everytime the application starts in any given timezone. Reason being any machine in any part of world can support any other timezone application.

I am trying reating a date with only time and another date with now(). Then appending the time from first date.

 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

s ravi chandran wrote:. . .I am trying [c]reating a date with only time and another date with now(). Then appending the time from first date.

Why? That sounds a very strange way to do things.

I am afraid your explanation is by no means clear, and I still don't know what you are trying to do. Please explain what you are trying to do; so far you are giving little bits of how, which doesn't help us.
 
s ravi chandran
Ranch Hand
Posts: 595
6
jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not sure how to put it. Basically my time is fixed and date is variable. The task is supposed to be triggered at this particular time everday. No matter when the application started.

So i take the duration between time when application started and time when that task is to be triggered and schedule it. I thought of getting new Date() and setting task time to this and convert it to instant.

But I think there should be a better way to do this.

Does this make my problem clear?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is still not very clear what exactly you are looking for. Maybe that's the whole problem - because you don't know how to explain exactly what you want, you also can't put it into code because it's also not really clear to yourself.

Java has a very good date and time API since Java 8; there are many classes in the package java.time to work with dates, times, durations, etc.

There are a few principles you need to understand. An instant is an "absolute" point in time which happens exactly once (a specific moment). An instant can be expressed as a specific date and time in any timezone. What exactly the day, month, year, hour, minute, second is depends on in what timezone you want to express it. For example, 10-10-2017 15:30:00 in the UTC timezone is the same instant as 10-10-2017 17:30:00 in the Europe/Paris timezone.

Some temporal objects represent instants, and some do not. A LocalDate, LocalTime or LocalDateTime is not an instant, because it does not refer to a specific moment. These are objects that do not hold a complete set of information, just a subset (for example, only day / month / year, or only hours / minutes / seconds / milliseconds).

There are special classes for holding amounts of time: Duration and Period.

Study the classes in the package java.time are carefully find out what the purpose of each class is. Then think about your problem and figure out how you can solve it using the java.time classes.

It sounds like you maybe need to use a Duration or a Period and add it to a specific instant, to get another instant. Look at the methods that are available for doing date and time calculations.

Examples:

 
Stephan van Hulst
Saloon Keeper
Posts: 15485
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, I think I understand what you mean.

You want a certain task to run every day at a certain time. The user enters that time as a local time in a certain time zone. Every day, you can do this:
 
s ravi chandran
Ranch Hand
Posts: 595
6
jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Okay, I think I understand what you mean.

You want a certain task to run every day at a certain time. The user enters that time as a local time in a certain time zone. Every day, you can do this:



Thanks for the response. Sorry for the obscurity. I was typing all this from my phone, hence no code possible.

I did solve it eventually similar to what you suggested. Your code looks much precise.

I tried this:

Will update my code with your solution.

My solution also handles the case where application starts after the userEnteredTime. I think this should be the same for your solution also.
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I notice in your example you are using GMT.
What happens between the end of March and the end of October each year?
Is the user going to be OK with whatever it is running an hour later than they had specified?
 
Stephan van Hulst
Saloon Keeper
Posts: 15485
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your code is not correct. The application time consists of the wrong date if LocalDate.now() is a different date than the date of the current day in the entered time zone. You need to use the current date of the time zone.

You don't need to jump over difficult hurdles to get the current time. Just use Instant.now().
 
s ravi chandran
Ranch Hand
Posts: 595
6
jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Tolls wrote:I notice in your example you are using GMT.
What happens between the end of March and the end of October each year?
Is the user going to be OK with whatever it is running an hour later than they had specified?


That is just test value. Actual timezone will be read from configuration which application uses to startup.

Also, these settings change as per required. Like EDT to EST changes.
 
s ravi chandran
Ranch Hand
Posts: 595
6
jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Your code is not correct. The application time consists of the wrong date if LocalDate.now() is a different date than the date of the current day in the entered time zone. You need to use the current date of the time zone.

You don't need to jump over difficult hurdles to get the current time. Just use Instant.now().


Thanks. I was actually focussing on getting ZonedDateTime to have the correct time. I am using your logic now.
 
Stephan van Hulst
Saloon Keeper
Posts: 15485
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What Dave means is, what do you intend your application to do when the user has selected a time zone that uses daylight saving time, and has selected a time that is either invalid for the date the clock is adjusted, or ambiguous?

For instance, if the user has selected 2:30 AM as the time to perform the task, in the timezone Europe/Amsterdam, what do you expect your application to do on October 29th 2017? And on March 25th 2018? Will the task be performed twice? Will it be performed at all? Will the application crash?
 
s ravi chandran
Ranch Hand
Posts: 595
6
jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:What Dave means is, what do you intend your application to do when the user has selected a time zone that uses daylight saving time, and has selected a time that is either invalid for the date the clock is adjusted, or ambiguous?

For instance, if the user has selected 2:30 AM as the time to perform the task, in the timezone Europe/Amsterdam, what do you expect your application to do on October 29th 2017? And on March 25th 2018? Will the task be performed twice? Will it be performed at all? Will the application crash?


For invalid time and timezone I take the system defaults.  The time check happens only once during application start, it will not repeat again for that day. The task will also trigger only once per application start. So after that whatever changes happen to clock adjustment, this check will not get triggered.
 
Stephan van Hulst
Saloon Keeper
Posts: 15485
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, but *when* will it trigger?

In the Netherlands, on the 29th of October 2017, the task could trigger an hour earlier than the user intended, if they entered 2:00 AM. On the 25th of March 2018, the task could trigger an hour later than the user intended.

Does an hour of difference matter? If the task doesn't have to run on the *exact* moment the user wanted, you can use the code I wrote. If it does matter, you need to think about your requirements more.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:. . . on the 29th of October 2017, the task could trigger an hour earlier than the user intended, if they entered 2:00 AM. On the 25th of March 2018, the task could trigger an hour later than the user intended. . . .

Enter 1:30am and there is a risk of the task never triggering at all. I shall leave verification of that assertion as an exercise for the reader.
 
Stephan van Hulst
Saloon Keeper
Posts: 15485
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Untrue. Look at the documentation for ZonedDateTime.of(LocalDate, LocalTime, ZoneId).

Even if it were the case, it would be for 1:30 AM in the Europe/London time zone. In the Europe/Amsterdam time zone, it would be 2:30 AM.
 
s ravi chandran
Ranch Hand
Posts: 595
6
jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I take the time difference between the application start time and current time and schedule the task. So, no matter what time variations happen after this scheduling, it won't effect the task execution.

The important aspect here is the duration between current time retrieved and user defined application time.
 
Stephan van Hulst
Saloon Keeper
Posts: 15485
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

s ravi chandran wrote:The important aspect here is the duration between current time retrieved and user defined application time.


The problem is that the user entered time isn't well defined.

Once again I ask you, if the user has specified that every day the task must run at 2:30 AM in Europe/Amsterdam, what will happen when the application starts at 5:00 AM on the 25th of March 2018 on a machine in Asia/Kolkata?
 
Stephan van Hulst
Saloon Keeper
Posts: 15485
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to make the problem more clear to you, 2:30 AM on March 25th 2018 is not an existing point in time in Amsterdam, because after 1:59 AM the clock will read 3:00 AM.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I stand corrected; the zoned date time object corrects for the clocks going forward. So both these instances show the same time:-Note last year's clock change date used.
 
s ravi chandran
Ranch Hand
Posts: 595
6
jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got the point that time will shift at those particular instances.

I am not handling this edge case specifically.

The point being time duration is what effectively defines the trigger point. If application start time is already passed then difference will be some negative value, in which case I run the task immediately.

Exact time defined by user or current time do not have any direct implications on task trigger. Its just the difference in them that is used.

Campbell,
Can I conclude that ZonedDateTime by itself provides time adjustment to us without any intervention from our end?
 
Stephan van Hulst
Saloon Keeper
Posts: 15485
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your post does not make sense to me. Are you saying that you don't care about the exact difference in time? The difference in time depends on the date, time and time zone. If you care about the exact difference, you should care about time zone rules.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

s ravi chandran wrote:. . . Campbell,
Can I conclude that ZonedDateTime by itself provides time adjustment to us without any intervention from our end?

Don't know; it would appear to do so when the clocks go forward, but I haven't tried it properly the other way.
 
Stephan van Hulst
Saloon Keeper
Posts: 15485
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The documentation for ZonedDateTime.of() explains exactly the steps it takes when dealing with ambiguous times.
 
s ravi chandran
Ranch Hand
Posts: 595
6
jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Your post does not make sense to me. Are you saying that you don't care about the exact difference in time? The difference in time depends on the date, time and time zone. If you care about the exact difference, you should care about time zone rules.



I do care about the time. I was only focusing more on getting exact duration at start and moving on from there.

As I said, this is an edge case which I did not specifically handle.

Will go through this scenario and how it is going to impact my application.
reply
    Bookmark Topic Watch Topic
  • New Topic