简体   繁体   中英

Java\Android timestamp returns wrong hour value

For some reason I get 2 hours in the clock while I've only been running it for few seconds. Here are the results of the time stamps and the code:

08-12 01:03:47.858: I/System.out(2856): 1344722627872
08-12 01:03:51.198: I/System.out(2856): 1344722631206
08-12 01:03:54.698: I/System.out(2856): 3334
08-12 01:03:54.758: I/System.out(2856): 02:00:03




public static String getDate(long milliSeconds, String dateFormat)  {

    DateFormat formatter = new SimpleDateFormat(dateFormat);

     Calendar calendar = Calendar.getInstance();
     calendar.setTimeInMillis(milliSeconds);
     return formatter.format(calendar.getTime());   }

This is the complete code with the calling:

// Click Listener for Check In\Out Button

    checkIn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // Check Out
            myVib.vibrate(10);
            if (clicked == true) {
                timeStampWhenOut = System.currentTimeMillis();
                killcheck = true;
                checkedStatus = "Check In";
                checkIn.setText(checkedStatus);
                long getShiftLength = ShiftLength(timeStampWhenIn,
                        timeStampWhenOut);
                System.out.println(timeStampWhenOut);
                System.out.println(getShiftLength);
                System.out.println(getDate(getShiftLength, "hh:mm:ss"));
                clicked = false;
                wasShift = true;

                // Check In
            } else if (clicked == false) {

                timeStampWhenIn = System.currentTimeMillis();
                System.out.println(timeStampWhenIn);
                checkedStatus = "Check Out";
                checkIn.setText(checkedStatus);
                killcheck = false;
                clicked = true;

            }
        }
    });

Why are the hours wrong?

Assuming 3334 is an elapsed time, you're setting the Calendar to 3.334 seconds since the epoch (approximately midnight GMT on December 31st 1970). Then you're formatting that Calendar. And it's telling you that it's 2am plus 3 seconds in your time zone, by which I guess that your time zone is GMT+2.

Don't use Calendar and SimpleDateFormatter for elapsed times - it's not the right tool for the job. Have a look at DurationFormatUtils in Apache-Commons instead.

The code is working fine for me. Could you show where you call the getDate method? Here's how im using it:

System.out.println(getDate(System.currentTimeMillis(), "hh:mm:ss"));

And that's logging the right time (current for me 6:10:29)

EDIT: Why are you even using the calendar in the middle? You hand your method milliseconds, which it puts in a calendar, then takes right back out.

public static String getDate(long milliSeconds, String dateFormat)  {

    DateFormat formatter = new SimpleDateFormat(dateFormat);

     return formatter.format(milliSeconds);
}

^ That would be much better

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM