java获取北京时间系统时间全球各地时间。

编程入门 行业动态 更新时间:2024-10-22 21:40:53

java获取北京<a href=https://www.elefans.com/category/jswz/34/1771441.html style=时间系统时间全球各地时间。"/>

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获取北京时间系统时间全球各地时间。

本文发布于:2023-06-30 16:27:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/963080.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时间   北京时间   全球   系统   java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!