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

  • Change Icon Of Navigation Drawer
  • Send location updates to IntentService
  • Error : No resource found that matches the given name (at 'icon' with value '@drawable/icon')
  • AsyncTask and Contexts
  • how to set the bitmap to the ImageView in main.xml captured from the camera?
  • @Named providers with same return types end up giving java.lang.IllegalArgumentException: Duplicate
  • Related posts:

    How to use onSavedInstanceState example please
    Could not fetch model of type 'IdeaProject' using Gradle installation
    Is there any point of an Activity with one fragment?
    ImageView in circular through xml
    How to obtain MAC address of WiFi network interface?
    Unable to start service with null
  • How to Integrate reCAPTCHA 2.0 in Android
  • Why is onLoadFinished called again after fragment resumed?
  • Multi-gradient shapes
  • Save File using Binary String on Android via HTML5
  • Sencha Touch 2.1: Form Panel Keyboard hides active Textfield on Android
  • Android: fragments overlapping issue
  • 4 Solutions collect form web for “Getting device's local timezone”

    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

    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.

    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
    

    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
    }
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.