• Anddoi 将时间转换为指定时区的时间


    import java.text.Format;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.Properties;
    import java.util.SimpleTimeZone;
    import java.util.TimeZone;

    import javax.swing.text.html.HTMLDocument.Iterator;


    public class Test2 {

    protected static Format format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
    * timeZoneOffset表示时区,如中国一般使用东八区,因此timeZoneOffset就是8
    * @param timeZoneOffset
    * @return
    */
    public String getFormatedDateString(int timeZoneOffset){
    if (timeZoneOffset > 13 || timeZoneOffset < -12) {
    timeZoneOffset = 0;
    }
    TimeZone timeZone;
    String[] ids = TimeZone.getAvailableIDs(timeZoneOffset * 60 * 60 * 1000);
    if (ids.length == 0) {
    // if no ids were returned, something is wrong. use default TimeZone
    timeZone = TimeZone.getDefault();
    } else {
    timeZone = new SimpleTimeZone(timeZoneOffset * 60 * 60 * 1000, ids[0]);
    }

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    sdf.setTimeZone(timeZone);
    return sdf.format(new Date());
    }

    public static String getFormatedDateString(String _timeZone) throws Exception{
    SimpleDateFormat s=new SimpleDateFormat("yyyy/MM/dd HH:mm");
    Date ddd=s.parse("2014/6/13 4:00");
    TimeZone timeZone = null;
    if("".equals(_timeZone)){
    timeZone = TimeZone.getDefault();
    }else{
    timeZone = TimeZone.getTimeZone(_timeZone);
    }

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
    sdf.setTimeZone(timeZone);
    //TimeZone.setDefault(timeZone);
    return sdf.format(ddd);
    }

    public static void setCNTimeZone(){
    final TimeZone zone = TimeZone.getTimeZone("GMT+8");
    TimeZone.setDefault(zone);
    }

    public static void main(String args[]) throws Exception{
    System.out.println(getFormatedDateString(""));
    System.out.println(getFormatedDateString("Asia/Shanghai"));
    System.out.println(getFormatedDateString("Asia/Seoul"));
    System.out.println(getFormatedDateString("Europe/Madrid"));
    System.out.println(getFormatedDateString("GMT+1:00"));
    System.out.println(TimeZone.getDefault().getID());
    }


    }

  • 相关阅读:
    mybatis-plus 相关
    Nginx 相关
    Docker 相关
    shiro & jwt
    Java GC
    C++ Q&A
    epoll ET & LT
    关于 free 命令显示内存使用情况问题
    Metaprogramming in Ruby: It’s All About the Self
    On The Value Of Fundamentals In Software Development (基础知识在软件开发中的价值)
  • 原文地址:https://www.cnblogs.com/horrywu/p/3765363.html
Copyright © 2020-2023  润新知