Get previous occurrence of a LocalTime

How to get the previous occurrence of a specified local time as an instant, for the system timezone ?

This basically means getting today at specified time or yesterday at specified time depending whether the specified time for today is before or after now.

Of course, I need to take into account timezone switch because of daylight saving. That is, the timezone offset can be different for today and yesterday.

This is what I've got now :

public Instant getPreviousOccurence(LocalTime scheduledTime) { Instant now =; Instant todayAtSpecifiedTime = now.with(scheduledTime); return todayAtSpecifiedTime.isAfter(now) ? todayAtSpecifiedTime.minus(1, ChronoUnit.DAYS) : todayAtSpecifiedTime; }

But after checking the source of Instant.minus(), I noticed that it removes 84600 seconds for a day, which would be wrong in my case. Additionally, I'm not sure whether Instant.with() will use the system timezone or UTC.

Edit 1

In case there are no occurrence of the specified time for today (because of a timezone shift), the instant of the timezone shift should be returned. In case there are two occurrences of the specified time for today, the latest in the past should be returned.

Edit 2

After checking with Product Owner, it turns out that in case there are two occurrences of the specified time within a day, always returning the first (or always returning the second) is fine. We don't need both.

-------------Problems Reply------------

Many thanks to Jon Skeet for pointing me to ZonedDateTime. Here is my solution using this type.

public Instant getPreviousOccurence(LocalTime scheduledTime) {
Instant now =;
ZonedDateTime todayAtScheduledTime = ZonedDateTime.ofInstant(now, EUROPE_PARIS).with(scheduledTime).withEarlierOffsetAtOverlap();
if (todayAtScheduledTime.toInstant().isAfter(now)) {
return todayAtScheduledTime.minusDays(1).withEarlierOffsetAtOverlap().toInstant();
} else {
return todayAtScheduledTime.toInstant();

Category:java Views:6 Time:2019-03-14
Tags: java time

Related post

  • Using Ruby to find the first previous occurrence of a string 2010-07-06

    I'm creating some basic work assistance utilities using Ruby. I've hit a problem that I don't really need to solve, but curiosity has the best of me. What I would like to be able to do is search the contents of a file, starting from a particular line

  • Jquery accordion, next and previous wizard, how to get previous and next sections? 2009-09-13

    I have a Jquery accordion which works fine. Sections get expanded/collapsed when clicked on respective headers. But I want to add the functionality so that when I click on "next" button it opens up the next section and clicking "Previous" button take

  • getting previously typed commands in python 2009-10-31

    I'm using python 2.5 in windows on a macbook pro with IDLE. How do I get previously typed commands in the python shell? In other operating systems I've managed to do this using 'ctrl' + 'up arrow' or a similar combination. I've tried all likely combi

  • How can I get previous and next objects from a filtered, ordered queryset? 2009-11-07

    I have a page based on a model object, and I want to have links to the previous and next pages. I don't like my current solution because it requires evaluating the entire queryset (to get the ids list), and then two more get queries. Surely there is

  • How do I get yesterday's date using localtime? 2010-08-17

    How do I tweak this to get yesterday's date using localtime? use strict; sub spGetCurrentDateTime; print spGetCurrentDateTime; sub spGetCurrentDateTime { my ($sec, $min, $hour, $mday, $mon, $year) = localtime(); my @abbr = qw( Jan Feb Mar Apr May Jun

  • how to get previous step values in next step in drupal multi step form 2010-11-03

    How to get previous step values in drupal in the next step in a multiform .I want to display the values other than drupal _set_message .I want to use these previous values in other function in the next step. --------------Solutions------------- try a

  • How do I get previous key from SortedDictionary? 2011-01-18

    I have dictionary containing key value pairs. SortedDictionary<int,int> dictionary=new SortedDictionary<int,int>(); dictionary.Add(1,33); dictionary.Add(2,20); dictionary.Add(4,35); I want to get previous key value pair from a known key v

  • How to get previous month and year relative to today, using strtotime and date? 2011-03-30

    I need to get previous month and year, relative to current date. However, see following example. // Today is 2011-03-30 echo date('Y-m-d', strtotime('last month')); // Output: 2011-03-02 This behavior is understandable (to a certain point), due to di

  • get previous row id using stored procedure 2011-05-17

    My requirement like this: I have to fetch a row from table "A". Now I have to loop through all the rows and get the values from the ID column. If I get ID = 5 then I want to execute some stored procedure through which I get the previous id row from s

  • Get all occurrences of a byte pattern with varying bytes from a byte array? 2011-05-21

    How can I match a byte array to a larger byte array and get the unique data and the location in the byte array that the pattern ends? FF FF FF FF XX XX XX XX FF FF FF FF (any length of any bytes goes here) 2E XX XX XX 00 I have the above pattern (whe

  • how to get previous control in c# 2011-07-30

    how to get previous control in c# there is a method to GetNextControl but there is no method to get previous control can any body tell me how could i get this thanx in advance --------------Solutions------------- GetNextControl(Control control, bool

  • How to get previous page URL in c# 2011-08-05

    How to get previous page URL from IE c#.. Request.UrlReferrer" not working in IE Please help... --------------Solutions------------- Try Request.ServerVariables["HTTP_REFERER"] But Request.UrlReferrer gets his data there too, so if the value isn't se

  • XSLT How to get previous attribute value if my condition satisfied? 2011-08-08

    XSLT How to get previous attribute value if my condition satisfied? I have tried to get the desired out but not succeeded can someone please assist me how to achieve this? Here is my Zip.xml <Zip> <ZipNotify Zip="1144" ZipNo="1" ZipTime="201

  • Get previous value of an entity 2011-09-22

    I need to get previous value of an entity. My requirement is like; I have some input fields in an edit page. 1 User can enter some values there and press save button at this time the user should be able to save it. 2 User can enter some values there

  • MSBuild: get previous version from msbuild script 2011-10-13

    Is there a posible to get previous version of code in TFS Build script? I need to get curren version of config file and version from previous check-in, compare them, check if some files in other folders have the same changes and save the results to l

  • How to get previous month and year 2012-01-18

    How to get previous month and year Code Dim dtr As String text1.Text = "01/2011" 'User Input.... (mm/yyyy) dtr = text1.Text If user selected 01/2011 then date should display previous month and year... Expected Output If user entered text1.text = "02/

  • Getting Previous Date Using Javascript 2012-02-08

    I want to get the before six months date using javascript. I am using the following method. var curr = date.getTime(); // i will get current date in milli seconds var prev_six_months_date = curr - (6* 30 * 24 * 60* 60*1000); var d = new Date(); d.set

  • How to get previous focussed element? 2012-03-11

    How to get previous focussed element? Explanation: When user clicks phonepad keys, it should not get active or focussed. so that: focus or activeElement returns previous focus element (textarea or input-text) . <script> $(function(){ $(".phonep

  • SQL Query Advice - Get Previous Customer Order 2008-12-12

    I have the following table custid ordid qty datesold 1 A2 12 2008-01-05 2 A5 5 2008-01-02 1 A1 5 2008-01-01 2 A7 3 2007-02-05 What't the best way of getting the previous order for every customer? Thanks --------------Solutions------------- If by "pre

  • Get previous and next row from current id 2009-02-27

    How can I do to get the next row in a table? `image_id` int(11) NOT NULL auto_increment `image_title` varchar(255) NOT NULL `image_text` mediumtext NOT NULL `image_date` datetime NOT NULL `image_filename` varchar(255) NOT NULL If the current image is

  • Get previous version of entity in Hibernate Envers 2009-04-27

    I have an entity loaded by Hibernate (via EntityManager): User u = em.load(User.class, id) This class is audited by Hibernate Envers. How can I load the previous version of a User entity? --------------Solutions------------- maybe this then (from Aud

  • MYSQL: Query to get previous and next video ID? 2009-07-04

    I am developing a video website (PHP - MYSQL), just like youtube, in which I want to provide the functionality of Next video and Previous video. Let's say I am currently on videoId: 234 so Next and Previous video links will point to videoId: 233 and

  • How do I get previous and next rows even though some rows have been deleted? 2009-08-04

    I have the following PHP functions that determine the next and previous rows in a database. However, there are lots of occasions when rows can be deleted and therefore my functions will not work as all they do is decrement the auto_increment field. F

  • Get previously entered information in Django FormWizard 2009-08-07

    I'm trying to create a simple Django multi-page form using FormWizard. What the form should do is the following: Let the visitor enter first and lastname in a form. Continue to next page where entered previously firstname and lastname will be display

  • Getting previously selected item in select box with jQuery 2009-12-04

    I have a single item HTML select box on which I'd like to find out the what item was selected just prior to changing selection to the new item. By the time the change event is fired, it's already too late: $("select").change(function () { var str = "

Copyright (C), All Rights Reserved.

processed in 0.182 (s). 11 q(s)