私はポストが投稿された時間を節約しているアプリケーションを開発しています。ユーザーのタイムゾーンを取得し、ユーザーのタイムゾーンに従ってデータベースに保存された時間を変換する方法は?詳細をご覧ください
私はこのコードを使用することにより、その時間を取得しています:
今DateFormat currentTime = new SimpleDateFormat("h:mm a");
final String time = currentTime.format(Calendar.getInstance().getTime());
、私がしたいことは、私は、ユーザーのタイムゾーンを取得し、彼/彼女に彼/彼女のタイムゾーンを使用してデータベースに保存された時間を変換したいれます現地時間。
私は、この使用してコードをやってみました:
public String convertTime(Date d) {
//You are getting server date as argument, parse your server response and then pass date to this method
SimpleDateFormat sdfAmerica = new SimpleDateFormat("h:mm a");
String actualTime = sdfAmerica.format(d);
//Changed timezone
TimeZone tzInAmerica = TimeZone.getDefault();
sdfAmerica.setTimeZone(tzInAmerica);
convertedTime = sdfAmerica.format(d);
Toast.makeText(getBaseContext(), "actual : " + actualTime + " converted " + convertedTime, Toast.LENGTH_LONG).show();
return convertedTime;
}
をが、これは時間を変更されていません。
String timeStr = postedAtTime;
SimpleDateFormat df = new SimpleDateFormat("h:mm a");
Date date = null;
try {
date = df.parse(timeStr);
} catch (ParseException e) {
e.printStackTrace();
}
convertTime(date);
は私が私のコードで間違っているものを知って聞かせたりしてください:
これは私が法の上使用してデータベースに保存された時間に変換しようとしている方法である(postedAtTime
は、データベースから取得取得された時間です)これは間違った方法ですか?
感謝。あなたはコードの一部をお勧めしますか? –
DBスキーマを変更できず、日付を文字列として保持する必要があると仮定します(回答の例) –
データベースを変更できます...私はこの作業をどのように行うことができますか? –