TimeZoneUtil 取国际地区时间,时间操作类

package com.dbutil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.SimpleTimeZone;
import java.util.TimeZone;

public class TimeZoneUtil {
    /**
     * 取北京时间,格式:yyyy-MM-dd HH:mm:ss
     * @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);
    }
    /**
     * 取澳大利亞时间
     * @return
     */
    public static String getAustraliaTime(){
        String time =getFormatedDateString(10);
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date=null;
        try {
            date = format.parse(time);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, -2);
        return format.format(calendar.getTime());
    }
    
    /**
     * 根据时间获取天数
     * @param
     * @author songanshu
     * @date 2018年8月28日 下午1:16:42
     */
    public static int getYear(String time) throws Exception {
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date=format.parse(time);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.YEAR);
    }
    /**
     * 根据时间获取月份
     * @param
     * @author songanshu
     * @date 2018年8月28日 下午1:16:42
     */
    public static int getMonth(String time) throws Exception {
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date=format.parse(time);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.MONTH)+1;
    }
    /**
     * 根据时间获取天数
     * @param
     * @author songanshu
     * @date 2018年8月28日 下午1:16:42
     */
    public static int getDay(String time) throws Exception {
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date=format.parse(time);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.DAY_OF_MONTH);
    }
    /**
     * 根据时间获取小时数
     * @param
     * @author songanshu
     * @date 2018年8月28日 下午1:16:42
     */
    public static int getHour(String time) throws Exception {
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date=format.parse(time);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.HOUR_OF_DAY);
    }
    /**
     * 根据时间获取分钟数
    * @param
    * @author songanshu
    * @date 2018年8月28日 下午1:16:42
     */
    public static int getMintue(String time) throws Exception {
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date=format.parse(time);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.MINUTE);
    }
    /**
     * 根据时间获取秒数
    * @param
    * @author songanshu
    * @date 2018年8月28日 下午1:16:42
     */
    public static int getSecond(String time) throws Exception {
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date=format.parse(time);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.SECOND);
    }
    /**
     * 根据指定日期获取 指定日期的毫秒数
    * @param
    * @author songanshu
    * @date 2018年8月30日 下午5:24:11
     */
    public static long getTimeLong(String time) throws Exception {
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date=format.parse(time);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.getTimeInMillis();
    }
    /**
     * 获取现在时间的毫秒数
    * @param
    * @author songanshu
    * @date 2018年8月30日 下午5:25:03
     */
    public static long getNowTimeLong() throws Exception {
        long nowDate=getTimeLong(getBeijingTime());
        return nowDate;
    }
    /**
     * 初始化期数  格式  yyyyMMddhhmm
    * @param
    * @author songanshu
    * @date 2018年8月28日 下午1:17:11
     */
    public static String  serializeSection(String time){
        String time0=time.substring(0,time.lastIndexOf(":"));
        String time1=time0.replaceAll("-"," ");
        String time2=time1.replaceAll(":"," ");
        String time3=time2.replaceAll(" ","");
        return time3;
    }
    
    /**
     * 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());
    }
    /**
     * 获取多少小时的时间
    * @param 多少小时
    * @author songanshu
    * @date 2018年8月28日 下午1:17:11
     */
    public static String  getHourDate(int hour){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date=new Date();//取时间
        Calendar calendar=new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.HOUR, hour);
        date=calendar.getTime();
        return sdf.format(date);
    }

/**
      * 指定时间与现在时间的差
     * @param
     * @author songanshu
     * @date 2018年10月21日 下午4:25:18
      */
     public static long getTimeDifference(String overTime) {//time 时间,dtime 倒计时(单位秒)
          SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
          Date nowDate= new Date();
          long second = 0;
             try {    
                 Date overDate= dfs.parse(overTime);     
                 long over=overDate.getTime();
                 long sy=over-nowDate.getTime();
                 if(sy>=0){
                    second= sy/ 1000;
                 }else{
                     second = 0;
                 }
                 } catch (Exception ex) {
                      ex.printStackTrace();
                 }
            return second;          
       }
     
     /**
      * 时间差
     * @param
     * @author songanshu
     * @date 2018年10月21日 下午4:25:18
      */
     public static long getTimeCha(String overTime) {//time 时间,dtime 倒计时(单位秒)
          SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
          Date nowDate= new Date();
          long second = 0;
             try {    
                 Date overDate= dfs.parse(overTime);     
                 long over=overDate.getTime();
                 long sy=nowDate.getTime()-over;
                 second= sy/ 1000;
                 } catch (Exception ex) {
                      ex.printStackTrace();
                 }
            return second;          
       }
    public static void main(String[] args) throws ParseException {
        SimpleDateFormat simple=new SimpleDateFormat("yyyyMMddHHmm");        
        String section= simple.format(new Date()); 
        System.out.println(section);
        
        Date date=new Date();
        System.out.println(date);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, +1);
        String nextSection= simple.format(calendar.getTime());
        System.out.println(nextSection);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值