Getting device's local timezone

32,118

Solution 1

Adding to what @org.life.java and @Erica said, here's what you should do

String dateStr = "2010-06-14 02:21:49-0400";
SimpleDateFormat sdf =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ssZ");
TimeZone tz = TimeZone.getDefault();
sdf.setTimeZone(tz);
Date date = sdf.parse(dateStr);

sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss a");
String newDateStr = sdf.format(date);

System.out.println(newDateStr);

Then newDateStr will be your new date formatted string.


UPDATE @xydev, the example I gave you works, see the full source code below:

/**
 * 
 */
package testcases;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

/**
 * @author The Elite Gentleman
 *
 */
public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try {
            String dateStr = "2010-06-14 02:21:49-0400";
            SimpleDateFormat sdf =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ssZ");
            TimeZone tz = TimeZone.getDefault();
            sdf.setTimeZone(tz);
            Date date = sdf.parse(dateStr);

            sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss a");
            String newDateStr = sdf.format(date);

            System.out.println(newDateStr);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

Output: 2010-06-14 08:21:49 AM

Solution 2

Using SimpleDateFormat

String string1 = "2010-06-14 02:21:49-0400";
SimpleDateFormat sdf =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ssZ")
sdf.setTimeZone(tz);
Date date = sdf.parse(string1);

Note: I am not sure the same class is available in andriod.

Solution 3

You can do all sorts of fancy formatting and localisation of dates using the DateFormat class. There's very good, complete documentation at the start of the API page here: http://download.oracle.com/javase/1.4.2/docs/api/java/text/DateFormat.html

Most regular cases can be handled with the built in SimpleDateFormat object. Its details are here: http://download.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html

The SimpleDateFormat output pattern string for the example you have above would be:

yyyy-MM-dd hh:mm a

Solution 4

Be careful that if you are running on the emulator your timezone is always GMT. I just spent 2 hours in trying to understand why my application does not give me the right time (the one my computer displays) until I realized it is because of the emulator.

To check you are running on the emulator use

if (Build.PRODUCT.contains("sdk")){
  // your code here for example if curtime has the emulator time 
  // since midnight in milliseconds then
  curtime += 2 * 60 * 60 * 1000;   // to add 2 hours from GMT
}
Share:
32,118
xydev
Author by

xydev

iPhone/Android Developer

Updated on September 13, 2020

Comments

  • xydev
    xydev over 3 years

    2010-06-14 02:21:49+0400 or 2010-06-14 02:21:49-0400

    is there a way to convert this string to the date according to the local machine time zone with format 2010-06-14 02:21 AM