问题
I'm developing an app in which I'm saving the date and time in Firebase
and then retrieving it for the user to see.
What I want is I want user to see that time and date according to their local timezone. for ex - if the time and date saved in database is '2:07 PM' and 'Fri, Jun 24', then to the users in PST timezone, it should be shown as '1:37 AM' and 'Fri, Jun 24'.
Here's how I'm getting time and date:
DateFormat currentTime = new SimpleDateFormat("h:mm a");
final String time = currentTime.format(Calendar.getInstance().getTime());
DateFormat currentDate = new SimpleDateFormat("EEE, MMM d");
final String date = currentDate.format(Calendar.getInstance().getTime());
How can I achieve this?
Please let me know.
回答1:
Try this:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
simpleDateFormat.setTimeZone(TimeZone.getDefault());
Date myDate = simpleDateFormat.parse("DateAndTimeToBeProcessed");
来源:https://stackoverflow.com/questions/38009126/how-to-show-the-time-and-date-saved-in-my-database-to-the-user-according-to-thei