qq_23367963
qq_23367963
采纳率0%
2019-01-24 13:28

utcTime Z后边有很多非法字符,解析竟然完全没有错误,是为什么呢?

   public static void main(String[] args) throws ParseException {
        String utcTime = "2018-01-31T14:32:19Zsdfsdfsssss";
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
//设置时区UTC
        df.setTimeZone(TimeZone.getTimeZone("UTC"));
//格式化,转当地时区时间
        Date after = df.parse(utcTime);
        System.out.println(after);
    }

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • Lxinccode Lxinccode 2年前
       /**
         * 本地时间转 UTC 时间字符串
         *
         * @param date
         * @return
         */
        public static String localToUtcString(Date date, String pattern) {
            SimpleDateFormat sdf = getSdf(pattern);
            sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
            return sdf.format(date);
        }
    
        /**
         * UTC 时间反格式化
         *
         * @param date
         * @param pattern
         * @return
         */
        public static Date utcStringToUtcDate(String date, String pattern) {
            SimpleDateFormat sdf = getSdf(pattern);
            sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
            Date utcDate = null;
            try {
                utcDate = sdf.parse(date);
            } catch (Exception e) {
    
            }
            return utcDate;
        }
    
        /**
         * UTC 时间格式化
         *
         * @param date
         * @return
         */
        public static String utcDateToUtcString(Date date) {
            SimpleDateFormat sdf = getSdf("yyyy-MM-dd'T'HH:mm:ss'Z'");
            sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
            return sdf.format(date);
        }
    
    
        /**
         * UTC 时间字符串转本地时间
         *
         * @param date
         * @param pattern
         * @return
         */
        public static Date utcStringToLocalDate(String date, String pattern) {
            SimpleDateFormat sdf = getSdf(pattern);
            sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
            Date localDate = null;
            try {
                localDate = sdf.parse(date);
            } catch (Exception e) {
    
            }
            return localDate;
        }
    
    

    时间格式化及完整类
    https://blog.csdn.net/Lxinccode/article/details/79414292

    点赞 评论 复制链接分享

相关推荐