Reputation: 3962
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;
}
}
Upvotes: 0
Views: 83
Reputation: 4292
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());
}
Upvotes: 1