141

i am trying to save my file with name as current date and time in milliseconds. and while reading file i want to read latest one.Here is the code

 Calendar rightNow = Calendar.getInstance();

  long offset = rightNow.get(Calendar.ZONE_OFFSET) +  rightNow.get(Calendar.DST_OFFSET);

  String sinceMidnight = Long.toString((rightNow.getTimeInMillis() + offset) %  (24 * 60 * 60 * 1000));

  return sinceMidnight+"_"+Filename;
Supreet
  • 2,451
  • 8
  • 25
  • 42

4 Answers4

350

I think leverage this functionality using Java

long time= System.currentTimeMillis();

this will return current time in milliseconds mode . this will surely work

long time= System.currentTimeMillis();
android.util.Log.i("Time Class ", " Time value in millisecinds "+time);

Here is my logcat using the above function

05-13 14:38:03.149: INFO/Time Class(301): Time value in millisecinds 1368436083157

If you got any doubt with millisecond value .Check Here

EDIT : Time Zone I used to demo the code IST(+05:30) ,So if you check milliseconds that mentioned in log to match with time in log you might get a different value based your system timezone

EDIT: This is easy approach .but if you need time zone or any other details I think this won't be enough Also See this approach using android api support

edwin
  • 7,985
  • 10
  • 51
  • 82
  • This is the most unreliable way to get the current time considering that different systems will return a different value. The documentation is quite explicit about it – Leo Nov 23 '18 at 02:13
  • @Leo hope you have read my edits , which is same point you mentioned. – edwin Feb 14 '19 at 08:58
24

The problem is that System. currentTimeMillis(); returns the number of milliseconds from 1970-01-01T00:00:00Z, but new Date() gives the current local time. Adding the ZONE_OFFSET and DST_OFFSET from the Calendar class gives you the time in UTC.

Calendar rightNow = Calendar.getInstance();

// offset to add since we're not UTC

long offset = rightNow.get(Calendar.ZONE_OFFSET) +
    rightNow.get(Calendar.DST_OFFSET);

long sinceMidnight = (rightNow.getTimeInMillis() + offset) %
    (24 * 60 * 60 * 1000);

System.out.println(sinceMidnight + " milliseconds since midnight");
logcat
  • 3,435
  • 1
  • 29
  • 44
Sunil Kumar
  • 7,086
  • 4
  • 32
  • 50
  • 1
    how does this answer differ from OP? – Istiaque Ahmed Jun 18 '16 at 09:42
  • @sunil kumar rightNow.getTimeInMillis() will return you local time in Milli Seconds so I believe offset will be subtracted from rightNow.getTimeInMillis() else this will not work for time zones with (GMT+N). long sinceMidnight = (rightNow.getTimeInMillis() + offset) % (24 * 60 * 60 * 1000); – muak Sep 06 '17 at 12:49
3

try this

  Calendar c = Calendar.getInstance(); 
  int mseconds = c.get(Calendar.MILLISECOND)

an alternative would be

 Calendar rightNow = Calendar.getInstance();

 long offset = rightNow.get(Calendar.ZONE_OFFSET) +
        rightNow.get(Calendar.DST_OFFSET);
 long sinceMid = (rightNow.getTimeInMils() + offset) %
       (24 * 60 * 60 * 1000);

  System.out.println(sinceMid + " milliseconds since midnight");
Rachel Gallen
  • 27,943
  • 21
  • 72
  • 81
  • see this reference it may help you http://developer.android.com/reference/android/os/SystemClock.html#setCurrentTimeMillis%28long%29 – Rachel Gallen May 13 '13 at 08:02
-1
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
int mSec = calendar.get(Calendar.MILLISECOND);
lokoko
  • 5,785
  • 5
  • 35
  • 68