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 8594051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:07:07+00:00 2026-06-12T00:07:07+00:00

Im creating a Fake-time or TimeSource for my application, the point is that this

  • 0

Im creating a “Fake-time” or TimeSource for my application, the point is that this new class should control the application’s actual time. This means all the methods that uses Date() constructor, System.currentTimeInMillis(), or Calendar.getInstance(), should instead use my class (ex. TimeSource.getInstance.getCurrentTimeInMillis()).

This way i can manipulate the system’s actual time when in a testing environment.

But i wonder if i should synchronize these methods, or how i should to it.

I can show you the following code:

public class DateTime {

private static final long ONE_MINUTE = 60 * 1000;
private static final long ONE_HOUR = ONE_MINUTE * 60;
private static final long ONE_DAY = ONE_HOUR * 24;
private static final long ONE_MONTH = ONE_DAY * 30;
private static final long ONE_YEAR =  ONE_DAY * 364;

private static DateTime instance;

private Date date;
private Date staticDate;

private boolean isStatic = false;

private long addedTimeInMilliseconds = 0;

public static DateTime getInstance() {
    if (instance == null) {
        instance = new DateTime();
    }
    return instance;
}

private DateTime() {
    date = new Date(System.currentTimeMillis());
    staticDate = new Date(System.currentTimeMillis());
}

public Date getCurrentTime() {
    if (isStatic) {
        return staticDate;
    } else {
        date.setTime(System.currentTimeMillis()+this.addedTimeInMilliseconds);
        return date;
    }
}

public long getCurrentTimeInMillis() {
    if (isStatic) {
        return staticDate.getTime();
    } else {
        return System.currentTimeMillis()+this.addedTimeInMilliseconds;
    }
}

}

Ive run some tests on the methods – both single and multithread tests – and the performance with syncronized methods is alot worse than without the synchronized. Problem is that im unsure if the methods might make problems in the future if i do not synchronize them.

I also use a few methods to add to the “current application’s time”

public void addMinutesToDateTime(int minutes) {
     this.addedTimeInMilliseconds += (minutes * ONE_MINUTE);
 }

So im asking a little advice, or knowledge about synchronized, and weather or not i should use it on my methods.

Best regards – Martin.

  • 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-06-12T00:07:08+00:00Added an answer on June 12, 2026 at 12:07 am

    I would have only one thread updating the time and make the fields volatile if needed.

    I wouldn’t use Date in a mutable way, I would use long.

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

Sidebar

Related Questions

Hey all, brand new to asp.net mvc and I am creating a fake social
Creating a class at runtime is done as follows: klass = Class.new superclass, &block
Creating liquid layouts is an immense pain. Now, I totally understand that tables should
I'm trying to use the code from this article: Creating Fake Enums , but
I'm creating a simple application that simply populates a DataGridView with a strongly typed
So I have this code creating a fake directory Options +FollowSymLinks RewriteEngine On RewriteCond
I am currently creating an application that uses OmniAuth to create and authenticate users.
I was wondering about creating fake URL queries in a web application. When you
I am having problems creating a new instance of a class by type with
(creating a separate question after comments on this: Javascript redeclared global variable overrides old

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.