0

I have the following code below in the server stored as gmt and would like it to change based on android device timezone.I am getting wrong value unable to figure out the mistake.I really appreciate any help.
Thanks in Advance.

public class MainActivity extends Activity {

    TextView t;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String d="2014-01-14 16:28:50";
        t=(TextView)findViewById(R.id.textView1);
        Timestamp ts = Timestamp.valueOf(d);    
        long tsTime1 = ts.getTime();
        String r=getDate(tsTime1);
        t.setText(r);
    }

    private String getDate(long timeStamp){
        SimpleDateFormat objFormatter = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
        objFormatter.setTimeZone(TimeZone.getDefault());

        Calendar objCalendar =    
            Calendar.getInstance(TimeZone.getDefault());
        objCalendar.setTimeInMillis(timeStamp*1000);//edit
        String result = objFormatter.format(objCalendar.getTime());
        objCalendar.clear();
        return result;         
    }
}
Uli Köhler
  • 13,012
  • 16
  • 70
  • 120
jason
  • 3,932
  • 11
  • 52
  • 123
  • Please fix your code, the function before `getDate()` seems incomplete and weirdly formatted to me. Thanks! – Uli Köhler Jan 30 '14 at 14:19
  • Formatting looks fine, thanks ;-) But there is definitely something missing right at the beginning (function declaration), see the unmatched closing curly bracket just before `getDate()`. – Uli Köhler Jan 30 '14 at 14:23
  • added complete code now.Do you know why I am getting wrong value? – jason Jan 30 '14 at 14:25

1 Answers1

1
public String TimeFormating(String Time)
 {
     SimpleDateFormat format_before = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss");
     SimpleDateFormat format_to_Convert = new SimpleDateFormat("hh:mm a",Locale.getDefault());

     format_before.setTimeZone(TimeZone.getTimeZone("GMT"));
     format_to_Convert.setTimeZone(TimeZone.getDefault());
     Date time = null;
     try 
     {    
         time = format_before.parse(Time);

     } catch (ParseException e) 
     {
         e.printStackTrace();
     }

     return format_to_Convert.format(time).toLowerCase(Locale.getDefault());

 }
Sagar Shah
  • 4,272
  • 2
  • 25
  • 36
  • Hi Sagar.I have used: `SimpleDateFormat format_before = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss"); SimpleDateFormat format_to_Convert = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss a",Locale.getDefault());` I dont get the month right it says **58** .The rest is correct.CAn you please fix that too.Thanks again. – jason Jan 30 '14 at 14:40