安卓

方法一:

SimpleDateFormat dff = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
dff.setTimeZone(TimeZone.getTimeZone("GMT+08"));
String ee = dff.format(new Date());
这个方法获取的结果是24小时制的,月份也正确

方法二:

Calendar calendar = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
String rt = sdf.format(calendar.getTime());

这个方法获取的结果是不是24小时制的,月份也正确

方法三:

public static String getLocalDatetimeString(String local) {
Calendar cal = new GregorianCalendar(TimeZone.getTimeZone(local));
cal.setTimeInMillis(Calendar.getInstance().getTimeInMillis());
String date = cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-" + cal.get(Calendar.DAY_OF_MONTH);
String time = cal.get(Calendar.HOUR_OF_DAY) + ":" + cal.get(Calendar.MINUTE) + ":" + cal.get(Calendar.SECOND);
return date + " " + time;
}
String tt = getLocalDatetimeString("GMT+8");

 代码里也看出来了,这个在月份上加了一个1, 24小时制

网上流传的还有这个方法:

Time t=new Time("GMT+8"); // or Time t=new Time("GMT+8"); 加上Time Zone资料。

t.setToNow(); // 取得系统时间。
int year1 = t.year;
int month1 = t.month;
int date2 = t.monthDay;
int hour = t.hour; // 0-23
int minute = t.minute;
int second = t.second;

这个月份上也是少了一个1,需要再加1,如 int month1 = t.month + 1;;

但是 hour 获取的值不对,不知为何

windows

flutter

flutter的 DataTime.parse(formattedString)能接收一个格式化后的时间(如 "2019-02-12 12:22:33"), 并返回DateTime格式的一个时间.

但是如何将一个utc格式的时间转为本地时间(北京时间,东8区时间)呢?

例如我需要将UTC时间 "2019-03-02T12:25:55.010Z"转化为(北京时间,东8区时间).

解决思路:

  1. 查看flutter的DataTime.parse的源代码,可以发现这一段注解

      • An optional time-zone offset part,
    • possibly separated from the previous by a space.
    • The time zone is either ‘z’ or ‘Z’, or it is a signed two digit hour
    • part and an optional two digit minute part. The sign must be either
    • "+" or "-", and can not be omitted.
    • The minutes may be separated from the hours by a ‘:’.
    • Examples: "Z", "-10", "01:30", "1130".
    • This includes the output of both [toString] and [toIso8601String], which
    • will be parsed back into a DateTime object with the same time as the
    • original.
    • The result is always in either local time or UTC.
    • If a time zone offset other than UTC is specified,
    • the time is converted to the equivalent UTC time.
    • Examples of accepted strings:
      • "2012-02-27 13:27:00"
      • "2012-02-27 13:27:00.123456z"
      • "2012-02-27 13:27:00,123456z"
      • "20120227 13:27:00"
      • "20120227T132700"
      • "20120227"
      • "+20120227"
      • "2012-02-27T14Z"
      • "2012-02-27T14+00:00"
      • "-123450101 00:00:00 Z": in the year -12345.
      • "2002-02-27T14:00:00-0500": Same as "2002-02-27T19:00:00Z"
        */

看到_`"2002-02-27T14:00:00-0500"`: Same as `"2002-02-27T19:00:00Z"`_这一段,就是说14点偏移往前5个小时就是19点了;

那么, 我需要转化UTC时间 "2019-03-02T12:25:55.010Z", 就必须要符合它上面列出来的格式.

首先要把".010Z"删除, 加上"-0800", ("-"代表向东边偏移,"+"代表向西边偏移,"08"代表8个小时,"00"代表分钟);

String utcTime="2019-03-02T12:25:55.010Z";
DataTime beijingTime=DateTime.parse("${utcTime.substring(0,19)}-0800");

参考资料:
https://blog.csdn.net/dearlaoyuan/article/details/9900967
https://zhuanlan.zhihu.com/p/58135333

发表评论

邮箱地址不会被公开。 必填项已用*标注