将日期转换为Java设备的本地时区。

huangapple go评论103阅读模式
英文:

Change Date to Local time zone of the device in Java

问题

我从数据库中获取到一个 Date 类型的值。这是一个示例:Wed Jul 29 13:03:00 GMT +03:00 2020。我有以下方法将其转换为字符串:

public static final String VIEW_DATE_FORMAT = "dd MMM yyyy HH:mm";

public static String dateToString(Date date){
    DateFormat dateFormat = new SimpleDateFormat(General.VIEW_DATE_FORMAT , Locale.getDefault());
    if(date == null)
        return "";
    return dateFormat.format(date);
}

我的问题是如何将日期转换为安装应用程序的设备的本地时区?我希望日期变成 Wed Jul 29 2020 16:03:00,但实际上得到的是 Wed Jul 29 2020 13:03:00

英文:

I'm getting a value of type Date from the Database. Here is an example Wed Jul 29 13:03:00 GMT +03:00 2020

and I have the following method to convert it to string

public static final String VIEW_DATE_FORMAT = "dd MMM yyyy HH:mm";

public static String dateToString(Date date){
    DateFormat dateFormat = new SimpleDateFormat(General.VIEW_DATE_FORMAT , Locale.getDefault());
    if(date == null)
        return "";
    return dateFormat.format(date);
}

My Question is how to convert the date to the Local time zone of the device the app is installed on ?
I want the date to become Wed Jul 29 2020 16:03:00 but I'm getting Wed Jul 29 2020 13:03:00 instead

答案1

得分: 1

如果您使用至少API级别26,则应使用ZonedDateTime类,然后可以将其转换为LocalDateTime

要将Date对象转换为ZonedDateTime,请参考Java8 java.util.Date转换为java.time.ZonedDateTime

要将ZonedDateTime转换为LocalDateTime,请参考在时区中将ZonedDateTime转换为LocalDateTime

英文:

If you are using at least API level 26 then you should use class ZonedDateTime which you can then convert to a LocalDateTime.

To convert a Date object to ZonedDateTime, refer to Java8 java.util.Date conversion to java.time.ZonedDateTime

To convert ZonedDateTime to LocalDateTime, refer to Convert ZonedDateTime to LocalDateTime at time zone

答案2

得分: 0

public String local_time(String date)
{
    Time time = new Time();
    try {
        date = date.replace("GMT +", "GMT+");
        time.set(new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy").parse(date).getTime());
    } catch (ParseException e) {
        e.printStackTrace();
    }

    String timeString = time.format("%a %b %d %H:%M:%S  %Z %Y");
    /* changing time zone */
    time.switchTimezone(Time.getCurrentTimezone());

    timeString = time.format("%a %b %d %H:%M:%S  %Z %Y");
    return timeString;
}

// Call it like:
// String loc_time = local_time("Wed Jul 29 13:03:00 GMT +03:00 2020");

Remember to import android.text.format.Time:

import android.text.format.Time;
英文:
  public String local_time(String date)
{

    Time time = new Time();
    try {
     date=  date.replace("GMT +","GMT+");
         time.set(new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy").parse(date).getTime());
    } catch (ParseException e) {
        e.printStackTrace();
    }
    
    String timeString =  time.format("%a %b %d %H:%M:%S  %Z %Y");
   // Log.e("Time string ",""+timeString);
   /* changing time zone */
    time.switchTimezone(Time.getCurrentTimezone());
   // Log.e("Time zone ",""+Time.getCurrentTimezone());

   
     timeString = time.format("%a %b %d %H:%M:%S  %Z %Y");
   // Log.e("New time ",""+timeString);
    return timeString;
}

call it like String loc_time=local_time("Wed Jul 29 13:03:00 GMT +03:00 2020");

remember to import android.text.format.Time : import android.text.format.Time;

huangapple
  • 本文由 发表于 2020年7月30日 16:40:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/63169378.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定