xydev
xydev

Reputation: 3409

Getting device's local timezone

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

Upvotes: 14

Views: 37934

Answers (4)

Zvi
Zvi

Reputation: 2424

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
}

Upvotes: 1

Buhake Sindi
Buhake Sindi

Reputation: 89209

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

Upvotes: 22

Erica
Erica

Reputation: 2261

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

Upvotes: 1

Jigar Joshi
Jigar Joshi

Reputation: 240996

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.

Upvotes: 7

Related Questions