How to show the time and date saved in my database to the user according to their local timezone? Please see details

前提是你 提交于 2019-12-12 03:02:25

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!