Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3481264
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:25:49+00:00 2026-05-18T10:25:49+00:00

My Java application needs to send an email out to all users once per

  • 0

My Java application needs to send an email out to all users once per day. I’d like to deliver it at approximately 2AM based on each user’s local time. So a user in New York would get the message at 2AM America/New_York time. A user in Los Angeles would get the message at 2AM America/Los_Angeles time. My mailing process would run once each hour of the day.

I plan to use HTML5 geo-location features or IP Address geo-locating to automatically determine each user’s time zone during sign up. Of course, the user can modify the timezone manually if these values are incorrect. I intend to store the selected timezone in the User object as a Timezone ID string, such as “America/New_York”. This will persist to the database as a string, but this could be changed if necessary.

Things to consider:

  • I need to account for daylight savings time, so the user always receives the email around 2AM. This means I can’t just store GMT-8 or similar UTC offsets in the user object. Part of the year Los Angeles is in GMT-7, and other parts it is in GMT-8.
  • Another SO question has answers that suggest storing offset information, but I don’t see how that would work as the times in different places can change throughout the year. I’m not going to update all my user objects timezones whenever a timezone change event somewhere in the world happens.
  • I am using JodaTime in my application, so I can take advantage of it if it will help.
  • I’m using Hibernate for database queries and would like to find a solution that could be handled in a hibernate query, without needing to process hundreds of thousands of user records in Java.

I’m using Spring Scheduling cron features (via Quartz) to run a method at 2 minutes past the hour every hour of the day. Right now most of my users are in America/Los_Angeles, so I’m manually forcing all mail to be sent at 2:02AM Pacific time. The following method runs every hour at :02 past the hour, but it manually checks the time and only proceeds if the current time in Los Angeles is in the 2:00 hour. This means users in other places will get the email at a different time than 2AM.

@Scheduled(cron="0 2 * * * *")
public void sendDailyReportEmail()  {
    DateTime now = new DateTime(DateTimeZone.forID("America/Los_Angeles"));
    if (now.getHourOfDay() != 2) {
        log.info("Not 2AM pacific, so skipping email reports");
        return;
    }
    // send email reports
}

So, what I need is to perform a database query that will give me all users that have a local time in the 2:00AM hour, based on the time zone in their User object. Any thoughts on how to accomplish this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T10:25:49+00:00Added an answer on May 18, 2026 at 10:25 am

    I’ve given this more thought and have a possible solution. I haven’t implemented it yet, but I think it should work. Basically, it involves performing the following query (NOTE: this is MySQL specific):

    select * from members where hour(convert_tz(now(),'UTC',timezone)) = 3;
    

    This query selects all members that have a current local time between 3AM and 3:59AM by doing the following:

    1. It gets the current server time using now(), which is a time in UTC since the database server is configured for UTC time.
    2. It then converts that time with the convert_tz() function to the timezone of the member as specified in the timezone column of the members table. Note that the Member class contains a timezone field in the format “America/Los_Angeles”.
    3. Next, it checks what hour it is with the hour() function to see if it is 3, for 3AM.
    4. The query then returns all members which meet these requirements and sends email to each of them.

    I’m a little concerned about doing this timezone conversion and calculation for every member in the system every hour. I’ll have to test things to see what kind of load it puts on the database, but it will probably choke with millions or even 10k’s of users.

    An alternative that should significantly lighten the load, but isn’t quite as elegant is to do the following (NOTE this code is UNTESTED!):

    @Scheduled(cron="0 2 * * * *")
    public void sendDailyReportEmail()  {
    
        Query query = session.createQuery(
            "select distinct m.timezone from Member m");
        List<String> timezones = query.list();
    
        for (String timezone : timezones) {
            DateTime now = new DateTime(DateTimeZone.forID(timezone));
            if (now.getHourOfDay() == 3) {
                Query query2 = session.createQuery(
                    "from Member where timezone = :zone");
                query2.setParameter("zone", timezone);
                List<Member> members = query2.list();
                // send email reports
            }
        }
    }
    

    This code does this:

    1. Execute method at 2 minutes past the hour, every hour of every day
    2. Query the database for a list of all unique timezones in the members table.
    3. For each timezone found, determine if it is currently in the 3AM hour.
    4. If a timezone is in the 3AM hour, select all of the members in that timezone and send email to them.

    This adds a little extra overhead at the beginning of each hour, but requires no per-user timezone processing. Chances are that there will only be a few dozen unique time zones actually used by the system. It’s very unlikely that I’ll have members in all 500+ timezones.

    One last thing. Instead of sending email during the 2AM hour, I moved it to 3AM. This is because when daylight savings time changes in the spring, time changes from 2AM to 3AM. So there is no such thing as 2:02AM, meaning emails wouldn’t get sent that day. Furthermore, in the fall, the system could send out 2 emails per user since an hour repeats itself. I need to find out if non-USA timezones change time at different times, because the 3AM hour might be a problem elsewhere.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to send email notifications in my Java web application. I'm using Apache
I'm having a problem with the following setup: A Java application send email msg
I'm looking for a java-based (preferable) mailing list application that allows users to opt-in/opt-out
I'm making a Java console application that needs to send an HTTP request to
I have a java application which needs to read and write files to HDFS.
I have a Java application that needs to collect and report a large number
I'm developing a Java application that uses java.util.logging for its logging needs. This application
I am writing a java application that takes schema-bounded XML as input and needs
I am developing a Java Desktop Application . This app needs a configuration to
We use Google Apps (Gmail) to send and receive all of our email. Our

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.