zoukankan      html  css  js  c++  java
  • java时区问题设置,new Date()和系统时间相差8个小时

    出现这种问题有可能是服务时间没有修改。

    import java.text.DateFormat;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;

    import java.util.Date;

    import java.util.TimeZone;

    public class GetNowDate {


    //获取系统当前时间,字符串类型
    public static String getStrDate(){
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    //设置为东八区
    sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
    Date newDate = new Date();
    String dateStr = sdf.format(newDate);
    return dateStr;
    }

    //获取系统当前时间Date类型,需要将字符串类型转成时间
    public static Date getDaDate(){
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    //设置为东八区
    sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
    Date date = new Date();
    String dateStr = sdf.format(date);

    //将字符串转成时间
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date newDate=null;
    try {
    newDate = df.parse(dateStr);
    } catch (ParseException e) {
    e.printStackTrace();
    }
    return newDate;
    }

    }

  • 相关阅读:
    mysql复习
    常用函数
    contos7上安装rabbitmq
    linux笔试题
    发布脚本
    Arch最小化安装LXDE桌面环境
    Arch最小化安装X
    Arch安装详解
    Gentoo解决Windows双系统时间不同步的问题
    Gentoo安装详解(五)-- 安装X桌面环境
  • 原文地址:https://www.cnblogs.com/guangxiang/p/11050890.html
Copyright © 2011-2022 走看看