码迷,mamicode.com
首页 > 其他好文 > 详细

取北京时间,纽约时间,班加罗尔时间

时间:2015-01-08 20:01:09      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:

    /**
     * 取北京时间
     * @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()); }

 

取北京时间,纽约时间,班加罗尔时间

标签:

原文地址:http://www.cnblogs.com/xiandedanteng/p/4211571.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!