Getting the current time and comparing it to a time in the future

Hello,
I am seeing in my research some debate on the best way to get the time. I am reading that just using the Date class and calling getTime() is not the most reliable way to do things. Overall, I am trying to get the current time down to the millisecond. I then want to add 5 seconds to that time and then store that value. FInally, when the current time is greater or equal to this stored value, I want to continue on in my program. Can anyone point me in the right direction on how to calculate the time variable? Any help would be much appreciated. I am new to the JAVA world.
Thanks,
Tim

To store the current time, simply use new java.util.Date(). Yes, there might be slight inconsistencies and it might not be the most accurate time possible, but without significant additional effort (perhaps a native O/S call or the use of another time library), you will not do much better. Calendar won't help in this instance either. The easiest way to get five seconds in the future is to do the following:
Date now = new Date();
Date future = new Date(now.getTime() + 5000);To store and retrieve the value, it depends on what you are doing. You can place it in the database via JDBC (BTW, if you trust the database timestamp more than Java's you can use something like Oracle's SYSDATE instead of Java's date in your SQL query). You can place it in the filesystem. You can store it in the user's session.
- Saish

Similar Messages

  • Just tried to update my iPad 2 to the current iOS, and I got an error that said the update wouldn't install. The iPad is stuck in a funny mode with the apple and spinning wheel showing. I can't power it down or restart it. What next? Thanks.

    Just tried to update my iPad 2 to the current iOS, and I got an error that said the update wouldn't install. The iPad is stuck in a funny mode with the apple and spinning wheel showing. I can't power it down or restart it. What next? Thanks.

    Try a hard reset: hold the power and home buttons simultaneously until the Apple logo appears.

  • How to get the current date and compare dates

    hi
    does anyone know how you would get the current date?
    furthermore, how to compare the current date with a past date to see how many days or months have past since then?
    thanks

    have a look at the Date class. new Date() creates an object containing the exact date and time at creation time. (Date).getTime() might be useful for comparing two dates. GregorianCalendar might be useful, too.

  • How to get the current data and time of SCOM server via SCOM SDK (API) calls?

    Hi,
    I need to read the current date and time of SCOM server via SOM SDK.
    Is there a class in SDK that provides this info ?
    Thanks,
    satheesh

    To get time and date of Alerts of SCOM, You can use following command let "get-scomalert"
    Also, You can refer below links
    http://blog.tyang.org/2013/02/21/using-scom-2012-sdk-to-retrieve-resource-pools-information/
    Please remember, if you see a post that helped you please click "Vote As Helpful" and if it answered your question, please click "Mark As Answer"
    Mai Ali | My blog: Technical

  • I deleted gardens of time yesterday and need to get it back with the current data and progress. Is this possible?

    I deleted gardens of time yesterday and need to get it back with the current data and progress. Is this possible?

    Is that on an iPad, iPod Touch or iPhone?

  • Get the Current Date and Time of a Location

    I have created a windowsPhoneApplication in silverLight 8.1.In my application,I have to upload videos on some server.The Uploading get failed If the Date and Time settings of my device is not the current Date and Time.So How can I get the current date
    and time even if the device settings is wrong?

    Hi shifana,
    Do you know server's time zone? If yes, you can manually translate to the correct time zone.
    Otherwise try some online time acquire API.
    --James
    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click
    HERE to participate the survey.

  • Put the current date and time in a string

    I am having trouble trying to put the current date and time from my Calendar object into a string
    any ideas?

    Calendar timeCal = Calendar.getInstance();
    timeCal.setTime(new Date(System.currentTimeMillis() - startTime ));
    String date = formatToTwoDigits(timeCal.get(Calendar.HOUR_OF_DAY))     + ":" +
    formatToTwoDigits(timeCal.get(Calendar.MINUTE))          + ":" +
    formatToTwoDigits(timeCal.get(Calendar.SECOND));
    private String formatToTwoDigits(int i) {
    String ret = null;
    i = i % 100;
    if (i < 10) {
    ret = "0" + Integer.toString(i);
    } else {
    ret = Integer.toString(i);
    return ret;
    You can figure out yourselve how to display the date, use other Calendar.XXX properties.
    Good luck!

  • I'm trying to display the current date and time on my iPad.

    First, I'm not sure this is a valid post to this forum.  If not, I'd appreciate knowg in where I can get help. 
    I have tried several ways to display the current date and time on my iPad.  None  work.  Most things I try give me complier errors but the latest code compiles but won't display the correct time. 
    Here's the dispatch code in my ViewController.m file:
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
        dispatch_async(queue, ^{
            [NSTimer scheduledTimerWithTimeInterval:NSTimeInterval)1.0f target:self selector:@selector(updateLabel:) userInfo:nil repeats:YES];
    Here's the updateLabel code: (which never gets executed).
    - (void)updateLabel:(id) sender {
        dispatch_sync(dispatch_get_main_queue(), ^{
        NSLog(@"Datefield Update Called");
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"EEEE, MMMM dd, yyyy 'at' HH:mm  ZZZZ"];
        NSDate *dayTime = [NSDate date];
        NSString *formattedDateString = [dateFormatter stringFromDate:dayTime];
            _dateField.text = formattedDateString;
        [self.view addSubview:_dateField];
        NSLog(@"Datefield %@",_dateField.text);

    Your code is needlessly complicated and has a few problems:
    // These objects should be created only once.
    NSTimer *dateAndTimeTimer; (You might need to make this a property with a strong reference).
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"EEEE, MMMMM dd, yyyy 'at' HH:mm ZZZZ"];
    // The subview should only be added once.
    [self.view addSubview:_dateField];
    [self performSelectorOnMainThread:@selector(startDateAndTimeTimer)
                            withObject:nil
                         waitUntilDone:YES];
    -(void)startDateAndTimeTimer
        NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1
                                                          target:self
                                                        selector:@selector(updateLabel)
                                                        userInfo:nil
                                                         repeats:YES];
        dateAndTimeTimer = timer;
        [[NSRunLoop mainRunLoop] addTimer:dateAndTimeTimer forMode:NSRunLoopCommonModes];
    -(void)updateLabel
         NSDate *dateTime = [NSDate date];
         NSString *formattedDataString = [dateFormatter stringFromDate:dateTime];
         _dateField.text = formattedDateString;
         NSLog(@"_dateField.text=%@", _dateField.text);

  • How to print the current date and time?

    Hi Friends!
    I have tried this program to print the current time and date,
    #include <iostream>
    #include <time.h>
    using namespace std;
    int main()
    time_t tim;
    time(&tim);
    cout << ctime(&tim);
    return 0;
    But it show the warning as;
    warning C4996: 'ctime': This function or variable may be unsafe. Consider using ctime_s instead.
    Can anyone suggest whats wrong with this?

    i also got many warring when i use old version keyword. as i note on think thatin new vc++ has many keyword change with _s. the old keyword works even get warring.or you can change to as it suggest.For About ctime_s
    Check this link for more detail (RefLink)
    No, it shows error, when you use ctime_s instead of ctime.
    error C2660: 'ctime_s' : function does not take 1 arguments

  • When I try to animate Nexrad radar on weatherunderground ..I get yesterdays radar and no animation. On Internet Explorer I get the current radar and animation.

    When I try to animate Nexrad radar on weatherunderground ..I get yesterdays radar and no animation. On Internet Explorer I get the current radar and animation.
    == URL of affected sites ==
    http://www.wunderground.com/radar/mixedcomposite.asp?region=b3&size=2x&type=loop

    You can try Firefox Safe Mode to see if one or more of your add-ons might be causing the problem. Also clearing the cookies for the site might help. The site loaded with current date and animation on my system.
    <u>'''Cookies'''</u>
    See:
    http://support.mozilla.com/en-US/kb/Deleting+cookies#Delete_cookies_for_a_single_site
    http://support.mozilla.com/en-US/kb/Cookies
    <u>'''Safe Mode'''</u>
    You may need to use '''[[Safe Mode]]''' (click on "Safe Mode" and read) to localize the problem. Firefox Safe Mode is a diagnostic mode that disables Extensions and some other features of Firefox. If you are using a theme, switch to the DEFAULT theme: Tools > Add-ons > Themes <u>'''before'''</u> starting Safe Mode. When entering Safe Mode, do not check any items on the entry window, just click "Continue in Safe Mode". Test to see if the problem you are experiencing is corrected.
    See:
    '''[[Troubleshooting extensions and themes]]'''
    '''[[Troubleshooting plugins]]'''
    '''[[Basic Troubleshooting]]'''
    If the problem does not occur in Safe-mode then disable all of your Extensions and Plug-ins and then try to find which is causing it by enabling <u>'''one at a time'''</u> until the problem reappears. <u>'''You MUST close and restart Firefox after EACH change'''</u> via File > Restart Firefox (on Mac: Firefox > Quit). You can use "Disable all add-ons" on the Safe mode start window.

  • Get the current version and level of sp.listitem

    I'm using a client script to get sp.listitem from a context.
    Everything works and load my item
    myCtxtWeb.Load(item);
    myCtxtWeb.ExecuteQuery();item.Versions;//doesn't existitem.Level;//doesn't exist
    Now i need to know the current version and the previous to compare the modified date between them.
    Unfortunately the property doesn't exist and same with property level for having the info if it's approved
    Are there a way to have them?

    For information, i found the solution
    SP.File newsItemFile = targetItem.File;
    SP.FileVersionCollection fileVersions = newsItemFile.Versions;
    myCtxtWeb.Load(fileVersions);
    myCtxtWeb.ExecuteQuery();
    if (fileVersions.Count > 0)
    SP.FileVersion currentVer = fileVersions[fileVersions.Count - 1];//get current version
    if (currentVer.VersionLabel == "1.0")
    return "add";
    Hope it helps some people

  • I've tried downloading iOS5 several times and after an hour each time I get a message that says my network connection has timed out.  But I can't find any information about "timing out" or how to correct the situation.  Any help?

    I've tried downloading iOS5 several times and after an hour each time I get a message that says my network connection has timed out.  But I can't find any information about "timing out" or how to correct the situation.  Any help?

    Disable your antivirus and firewall, and try again.

  • I connect my iphone 4 to my itunes and when I press update to get the new iOS 7 software, it says "This version of iTunes (10.6.3) is the current version" and it tells me to press ok. Why is my phone not updating??

    My phone says it is connected to itunes and beside the update button it says "a newer version of iphone software is available (version 7.0)...." but when I press 'update' a box pops up that says ""This version of iTunes (10.6.3) is the current version" and then I press okay and nothing else happens. I'm getting frustrated. if anyone can explain to me why I can't update please tell me! Or else I'll have to call up Apple I guess.
    thanks!

    You have answered your own question:
    "requires 10.6.3 or higher"
    " i upgraded my itunes to 10.5 "
    You need itunes 10.6.3 or later.

  • I put my new iPhone into iTunes for the first time, and chose to back it up to the iTunes i had on my iPod, but that made all my contacts on my sim card disappear, how can i get these contacts back?

    i put my new iPhone into iTunes for the first time, and chose to back it up to the iTunes i had on my iPod, but that made all my contacts on my sim card disappear, how can i get these contacts back?

    Have a read here...
    https://discussions.apple.com/message/18409815?ac_cid=ha
    And See Here...
    How to Use Multiple iDevices with One Computer

  • How do I get bookmarks on the toolbar to open in the current tab, and not open in a new tab?

    how do I get bookmarks on the toolbar to open in the current tab, and not open in a new tab?

    Start Firefox in <u>[[Safe Mode]]</u> to check if one of the extensions is causing the problem (switch to the DEFAULT theme: Firefox (Tools) > Add-ons > Appearance/Themes).
    *Don't make any changes on the Safe mode start window.
    *https://support.mozilla.com/kb/Safe+Mode
    *https://support.mozilla.com/kb/Troubleshooting+extensions+and+themes

Maybe you are looking for