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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:27:31+00:00 2026-06-13T17:27:31+00:00

Given: private Calendar calendarInstance = Calendar.getInstance(); public long inMillis() { calendarInstance.set(year, month, day, hour,

  • 0

Given:

private Calendar calendarInstance = Calendar.getInstance();

public long inMillis() {
    calendarInstance.set(year, month, day, hour, min);
    return calendarInstance.getTimeInMillis();
}

As i understand it, the result comes back with time since the epoch, in milliseconds

The current time as UTC milliseconds from the epoch.

Given that my test always sets the objects the same, why are results coming up different as time goes by?

detailedMoment = new MomentInTime(2012, 11, 1, 19, 9);
detailedMoment.inMillis() // gives different results as time passes by

UPDATE:

I continue to second guess myself due to
enter image description here

For the same time period i get

1_351_796_940 // http://www.epochconverter.com
1_354_410_540 // my number
  • 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-13T17:27:32+00:00Added an answer on June 13, 2026 at 5:27 pm

    I think you should use clear(). If you do that it will return you the exact number of miliseconds each time.

    public long inMillis() {
        calendarInstance.clear();
        calendarInstance.set(year, month, day, hour, min);
        return calendarInstance.getTimeInMillis();
    }
    

    From Java doc

    Sets all the calendar field values and the time value (millisecond offset from the Epoch) of this Calendar undefined. This means that isSet() will return false for all the calendar fields, and the date and time calculations will treat the fields as if they had never been set. A Calendar implementation class may use its specific default field values for date/time calculations. For example, GregorianCalendar uses 1970 if the YEAR field value is undefined.

    A Sample program

    public class MomentInTime {
    
    private static Calendar calendarInstance = Calendar.getInstance();
    
    public static long inMillis() {
        calendarInstance.clear();
        calendarInstance.set(2012, 10, 1, 19, 9);
        return calendarInstance.getTimeInMillis();
    }
    
    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 10; i++) {
            System.out.println(inMillis()/1000);
            Thread.sleep(300);
        }
    }
    }
    

    Output:

     1351777140
    

    enter image description here

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

Sidebar

Related Questions

Given the following types private class MyTestDummyClassValidationDef : ValidationDef<MyTestDummyClass> { ... } public class
Given the following multiton: public class Multiton { private static final Multiton[] instances =
Given a class such as: class Person { private: char *name; public: Person() {
Given this class public partial class Default : Page { private IRepository repo; ...
Given the following classes: public class WeekOfYear : IEquatable<WeekOfYear>, IComparable<WeekOfYear> { private readonly DateTime
Given the following code : public class Game { private Map<String,Coordinate> m_myHashMap; ... //
Given the following code : public abstract class Participant { private String fullName; public
Given the following classes: public class Container { private List<SomeData> list; // public getter
Given: public class TestSeven extends Thread { private static int x; public synchronized void
I have a google app engine application which needs to be given a public-private

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.