1

In my Android Application, I am trying to convert Date/Time to Milliseconds, check the below code:

public long Date_to_MilliSeconds(int day, int month, int year, int hour, int minute)
{

       Calendar c = Calendar.getInstance();
       c.setTimeZone(TimeZone.getTimeZone("UTC"));
       c.set(year, month, day, hour, minute, 00);

        return c.getTimeInMillis();

}

Problem: I am getting 1290455340800(Nov 22 14:49:00 EST 2010) for Nov 22 19:49:00 EST 2010 (i.e. 5 hours back)

FYI, I am Currently in Indian TimeZone, but application can be executed from any country. so How do i exact Convert the date/time into the Milliseconds?

Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
  • Is it the part of the same problem from http://stackoverflow.com/questions/4275823/problem-while-fetching-milliseconds-from-date/4275873#4275873? – Buhake Sindi Nov 26 '10 at 14:25

5 Answers5

1

My guess is that you're calling Date_to_MilliSeconds(22, 10, 2010, 19, 49). Your code explicitly uses UTC, so it's going to treat whatever you pass it in as UTC.

Just like your previous question (which makes me tempted to close this as a duplicate) it's unclear what you're really trying to do.

If you want to provide a local time to your method, you need to specify a local time zone. If you need to use a local time in the user's time zone, try setting the time zone to TimeZone.getDefault() - although I'd expect that to be the default anyway. If you want to provide a UTC time to your method, you need to specify a UTC time zone (as you are here).

What are you really trying to do?

Community
  • 1
  • 1
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • ya jon you are right, i am moving round and round with the "Timing" issue, what do i do to make an application as such as it will not get a Timing Issue?? – Paresh Mayani Nov 26 '10 at 14:43
  • @PM - Paresh: You need to be a lot clearer about what you actually mean at each step - what your input is (a local time in a particular time zone? a local time with no specific time zone? a UTC time?) and what output you need. – Jon Skeet Nov 26 '10 at 14:46
1

In this piece of code, you are getting the amount of milliseconds since 01/01/1970 00:00 in your timezone for Nov 22 19:49:00 EST 2010 in UTC timezone. Why are you setting timezone to UTC?

buddhabrot
  • 1,558
  • 9
  • 14
1

The 5 hours difference is the difference between UTC and EST. You can use DateFormat.parse() to parse the input date if it's a string. Or you can use the code above and pass the desired timezone in c.setTimeZone() -- put in EST instead of UTC.

cristis
  • 1,986
  • 16
  • 22
  • made my life!! but let me know, will i get problem while i will test it from other country than india? – Paresh Mayani Nov 26 '10 at 14:41
  • This should have nothing to do with the timezone you run the application in. If the source datetime has a timezone it will be parsed by `SimpleDateFormat.parse()` or you will use that in `setTimezone()`. – cristis Nov 26 '10 at 14:49
1

This line

c.setTimeZone(TimeZone.getTimeZone("UTC"));

Is probably causing the issue. There is no need to set the TimeZone as the current default is used.

Jim
  • 22,354
  • 6
  • 52
  • 80
  • ya you are right, i dont need to set the TimeZone, i have removed the TimeZone "UTC" and now its working fine, Actually i was trying to insert an Event into the Calendar, so now Time of event is inserting properly – Paresh Mayani Nov 27 '10 at 05:07
  • @PM - Paresh Mayani No problem :) – Jim Nov 27 '10 at 14:36
0

I'm using this:

public String timeToString(long time, String format) {
    SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.getDefault());
    sdf.setTimeZone(TimeZone.getDefault());
    return sdf.format(time + TimeZone.getDefault().getRawOffset()
            + TimeZone.getDefault().getDSTSavings());
}

I think it solves the TimeZone problems.

neteinstein
  • 17,529
  • 11
  • 93
  • 123