zoukankan      html  css  js  c++  java
  • 用Java取指定时区的时间 北京时间,纽约时间,班加罗尔时间

        /**
         * 取北京时间
         * @return
         */
        public static String getBeijingTime(){
            return getFormatedDateString(8);
        }
        
        /**
         * 取班加罗尔时间
         * @return
         */
        public static String getBangaloreTime(){
            return getFormatedDateString(5.5f);
        }
        
        /**
         * 取纽约时间
         * @return
         */
        public static String getNewyorkTime(){
            return getFormatedDateString(-5);
        }
        
        /**
    * 此函数非原创,从网上搜索而来,timeZoneOffset原为int类型,为班加罗尔调整成float类型 * timeZoneOffset表示时区,如中国一般使用东八区,因此timeZoneOffset就是8 * @param timeZoneOffset * @return */ public static String getFormatedDateString(float timeZoneOffset){ if (timeZoneOffset > 13 || timeZoneOffset < -12) { timeZoneOffset = 0; } int newTime=(int)(timeZoneOffset * 60 * 60 * 1000); TimeZone timeZone; String[] ids = TimeZone.getAvailableIDs(newTime); if (ids.length == 0) { timeZone = TimeZone.getDefault(); } else { timeZone = new SimpleTimeZone(newTime, ids[0]); } SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); sdf.setTimeZone(timeZone); return sdf.format(new Date()); }
  • 相关阅读:
    如何对一台服务器进行免密操作
    MySQL 中的自增主键
    mysql 添加多版本数据库
    Group by 优化
    join 查询优化
    CCF2020-09-Python题解
    CCF2019-09-Python题解
    CCF2019-12-Python题解
    CCF202006-Python题解
    差分约束
  • 原文地址:https://www.cnblogs.com/doudouxiaoye/p/5772893.html
Copyright © 2011-2022 走看看