我正在从数据库中获取一个Date数据类型,就像这样Mon Sep 14 11:30:00 GMT+03:00 2020.I想要将这个值更改为一个字符串,所以我使用了这个函数。
public static final String DATE_FORMAT = "yyyy-MM-dd HH:mm";
public static String dateToString(Date date){
    DateFormat dateFormat = new SimpleDateFormat(General.DATE_FORMAT);
    dateFormat.setTimeZone(TimeZone.getDefault());
    if(date == null)
        return "";
    return dateFormat.format(date);
}这个函数给了我这个输出2020-09-14 11:30,但是根据我的安卓设备时区,它应该是2020-09-14 02:30。有什么建议吗?
发布于 2020-09-15 16:25:53
试用Locale的getDefault()方法
public static String dateToString(Date date){
    DateFormat dateFormat = new SimpleDateFormat(General.DATE_FORMAT, Locale.getDefault());
    dateFormat.setTimeZone(TimeZone.getDefault());
    if(date == null)
        return "";
    return dateFormat.format(date);
}希望它能为你工作。谢谢。
https://stackoverflow.com/questions/63897101
复制相似问题