时间系统时间全球各地时间。"/>
java获取北京时间系统时间全球各地时间。
这里封装了一个类给大家直接上代码
package com.wz.orders.util;import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;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 GetTime {/*** 取北京时间,格式: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 blocke.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());}public static String getFormatedDateStringorder(float timeZoneOffset,int type){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]);}if(type==1){SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdHHmmss");sdf.setTimeZone(timeZone);return sdf.format(new Date());}else{long currentTime = System.currentTimeMillis() + 30 * 60 * 1000;SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/d HH:mm:ss");sdf.setTimeZone(timeZone);return sdf.format(new Date(currentTime));}}/*** 获取多少小时的时间* @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;}
}
根据自己的需求获取相应的时间即可
更多推荐
java获取北京时间系统时间全球各地时间。
发布评论