-
java 获取标准北京时间
2010-08-09 16:08:52java 获取标准北京时间 标准北京时间 北京时间 网络时间 internet时间 -
java获取标准格林尼治时间
2010-08-09 16:06:23java 获取标准格林尼治时 标准格林尼治时间 标准时间 -
java获取internet标准时间
2018-11-14 18:24:02java获取网络时间 import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.util.Date; import java.util.TimeZone; public ...java获取网络时间
import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.util.Date; import java.util.TimeZone; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub String webUrl = "http://www.baidu.com";// 百度 TimeZone.setDefault(TimeZone.getTimeZone("GMT+08:00")); // 时区设置(北京时间) try { URL url = new URL(webUrl);//取得资源对象 URLConnection conn = url.openConnection();//生成连接对象 conn.connect();//发出连接 long dateL = conn.getDate();//取得网站日期时间(时间戳) Date date = new Date(dateL);//转换为标准时间对象 //分别取得时间中的小时,分钟和秒,并输出 System.out.print(date.getHours()+"时"+date.getMinutes()+"分"+date.getSeconds()+"秒"); } catch (MalformedURLException e) {//url书写异常,就是没有书写正确的url e.printStackTrace(); } catch (IOException e) {// e.printStackTrace(); } } }
-
Java获取标准网络时间
2015-04-09 08:22:03网络上有个获取时间的代码如下: public static void main(String[] args) throws Exception{ //取得资源对象 URL url = new URL("http://www.bjtime.cn"); //生成连接对象 ...网络上有个获取时间的代码如下:public
static
void
main(String[] args)
throws
Exception{
//取得资源对象
URL url =
new
URL(
"http://www.bjtime.cn"
);
//生成连接对象
URLConnection uc = url.openConnection();
//发出连接
uc.connect();
long
time = uc.getDate();
System.out.println(
"long time:"
+time);
Date date =
new
Date(time);
System.out.println(
"date:"
+date.toString());
System.out.println(
new
SimpleDateFormat(
"yyyy-MM-dd hh-mm-ss"
).format(date));
}
刚开始还可以用,后来http://www.bjtime.cn这个不知为何挂了,所以我又找了一个获取北京时间的网址,这个是百度提供的,应该没那么容易挂吧http://open.baidu.com/special/time/,还是原来的代码,只是把url替换为http://open.baidu.com/special/time/就好了。 -
java如何获取当前日期和时间
2019-06-12 18:11:36获取标准时间可以通过System.currentTimeMillis()方法获取,此方法不受时区影响,得到的结果是时间戳格式的。例如: 1543105352845 我们可以将时间戳转化成我们易于理解的格式 SimpleDateFormat formatter= ...本篇博客主要总结java里面关于获取当前时间的一些方法
System.currentTimeMillis()
获取标准时间可以通过System.currentTimeMillis()方法获取,此方法不受时区影响,得到的结果是时间戳格式的。例如:
1543105352845
我们可以将时间戳转化成我们易于理解的格式
SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z"); Date date = new Date(System.currentTimeMillis()); System.out.println(formatter.format(date));
则该时间戳对应的时间为:
2018-11-25 at 01:22:12 CET
值得注意的是,此方法会根据我们的系统时间返回当前值,因为世界各地的时区是不一样的。
java.util.Date
在Java中,获取当前日期最简单的方法之一就是直接实例化位于Java包java.util的Date类。
Date date = new Date(); // this object contains the current date value
上面获取到的日期也可以被format成我们需要的格式,例如:
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss"); System.out.println(formatter.format(date));
Calendar API
Calendar类,专门用于转换特定时刻和日历字段之间的日期和时间。
使用Calendar 获取当前日期和时间非常简单:
Calendar calendar = Calendar.getInstance(); // get current instance of the calendar
与date一样,我们也可以非常轻松地format这个日期成我们需要的格式
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss"); System.out.println(formatter.format(calendar.getTime()));
上面代码打印的结果如下:
25-11-2018 00:43:39
Date/Time API
Java 8提供了一个全新的API,用以替换java.util.Date和java.util.Calendar。Date / Time API提供了多个类,帮助我们来完成工作,包括:
- LocalDate
- LocalTime
- LocalDateTime
- ZonedDateTime
LocalDate
LocalDate只是一个日期,没有时间。 这意味着我们只能获得当前日期,但没有一天的具体时间。
LocalDate date = LocalDate.now(); // get the current date
我们可以format它
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy"); System.out.println(date.format(formatter));
得到的结果只有年月日,例如:
25-11-2018
LocalTime
LocalTime与LocalDate相反,它只代表一个时间,没有日期。 这意味着我们只能获得当天的当前时间,而不是实际日期:
LocalTime time = LocalTime.now(); // get the current time
可以按如下方式format
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss"); System.out.println(time.format(formatter));
得到的结果类似如下:
00:55:58
LocalDateTime
最后一个是LocalDateTime,也是Java中最常用的Date / Time类,代表前两个类的组合 - 即日期和时间的值:
LocalDateTime dateTime = LocalDateTime.now(); // get the current date and time
format的方式也一样
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss"); System.out.println(dateTime.format(formatter));
得到的日期结果类似于:
25-11-2018 00:57:20
-
java获取网络时间_java获取网络当前时间
2021-02-12 10:31:40展开全部如果你要获取的是Internet时间,可以使用NTP服务。NTP概念简介Network Time Protocol(NTP)是62616964757a686964616fe4b893e5b19e31333332643164用来使计算机时间同步化的一种协议,它可以使计算机对其服务器...展开全部
如果你要获取的是Internet时间,可以使用NTP服务。
NTP概念简介
Network Time Protocol(NTP)是62616964757a686964616fe4b893e5b19e31333332643164用来使计算机时间同步化的一种协议,它可以使计算机对其服务器或时钟源(如石英钟,GPS等等)做同步化,它可以提供高精准度的时间校正(LAN上与标准间差小于1毫秒,WAN上几十毫秒),且可介由加密确认的方式来防止恶毒的协议攻击。
java实现:import java.io.InputStream;
import java.net.Socket;
public class TimeUtil {
public static final int DEFAULT_PORT = 37;//NTP服务器端口
public static final String DEFAULT_HOST = "time-nw.nist.gov";//NTP服务器地址
private TimeUtil() {
};
public static long currentTimeMillis(Boolean sync) {
if (sync != null && sync.booleanValue() != true)
return System.currentTimeMillis();
try {
return syncCurrentTime();
} catch (Exception e) {
return System.currentTimeMillis();
}
}
public static long syncCurrentTime() throws Exception {
// The time protocol sets the epoch at 1900,
// the java Date class at 1970. This number
// converts between them.
long differenceBetweenEpochs = 2208988800L;
// If you'd rather not use the magic number uncomment
// the following section which calculates it directly.
/*
* TimeZone gmt = TimeZone.getTimeZone("GMT"); Calendar epoch1900 =
* Calendar.getInstance(gmt); epoch1900.set(1900, 01, 01, 00, 00, 00);
* long epoch1900ms = epoch1900.getTime().getTime(); Calendar epoch1970
* = Calendar.getInstance(gmt); epoch1970.set(1970, 01, 01, 00, 00, 00);
* long epoch1970ms = epoch1970.getTime().getTime();
*
* long differenceInMS = epoch1970ms - epoch1900ms; long
* differenceBetweenEpochs = differenceInMS/1000;
*/
InputStream raw = null;
try {
Socket theSocket = new Socket(DEFAULT_HOST, DEFAULT_PORT);
raw = theSocket.getInputStream();
long secondsSince1900 = 0;
for (int i = 0; i
secondsSince1900 = (secondsSince1900 <
}
if (raw != null)
raw.close();
long secondsSince1970 = secondsSince1900 - differenceBetweenEpochs;
long msSince1970 = secondsSince1970 * 1000;
return msSince1970;
} catch (Exception e) {
throw new Exception(e);
}
}
}
中国大概能用的NTP时间服务器
server 133.100.11.8 prefer
server 210.72.145.44
server 203.117.180.36 //程序中所用的
server 131.107.1.10
server time.asia.apple.com
server 64.236.96.53
server 130.149.17.21
server 66.92.68.246
server www.freebsd.org
server 18.145.0.30
server clock.via.net
server 137.92.140.80
server 133.100.9.2
server 128.118.46.3
server ntp.nasa.gov
server 129.7.1.66
server ntp-sop.inria.frserver 210.72.145.44(国家授时中心服务器IP地址)
ntpdate 131.107.1.10
ntpdate -s time.asia.apple.com
-
java获取时间的方法是_java 获取当前时间的三种方法
2021-03-05 23:58:40总结java里面关于获取当前时间的一些方法System.currentTimeMillis()获取标准时间可以通过System.currentTimeMillis()方法获取,此方法不受时区影响,得到的结果是时间戳格式的。例如:1543105352845我们可以将... -
JAVA获取中国标准时间20210409T084227.128+0800
2021-04-09 16:47:08java获取中国标准时间网上介绍比较少,我就自己写了一个 DateFormat sdf1 = new SimpleDateFormat("yyyyMMdd'T'HHmmss.SSS+0800"); sdf1.setTimeZone(TimeZone.getTimeZone("GMT")); String date = sdf1.format... -
java 方法 时间_java 获取当前时间的三种方法
2021-03-21 09:42:07总结java里面关于获取当前时间的一些方法system.currenttimemillis()获取标准时间可以通过system.currenttimemillis()方法获取,此方法不受时区影响,得到的结果是时间戳格式的。例如:1543105352845我们可以将... -
java当前时间_java 获取当前时间的三种方法
2021-02-12 09:00:10总结java里面关于获取当前时间的一些方法System.currentTimeMillis()获取标准时间可以通过System.currentTimeMillis()方法获取,此方法不受时区影响,得到的结果是时间戳格式的。例如:1543105352845我们可以将... -
java 获取系统时间 8小时 jre_Java获取时间与系统时间相差8小时终极解决方案
2021-02-26 16:33:29GMT时间UTC时间是时间标准时间(Universal Time Coordinated),UTC是根据原子钟来计算时间,误差非常小。UTC也是指零时区的时间,如果要表示其他时区的时间,这里要注意没有UTC+0800或者UTC+8这样的表示方式(至少Ja..... -
mybatis中获取当前时间_java 获取当前时间的三种方法
2021-01-12 13:51:46总结java里面关于获取当前时间的一些方法system.currenttimemillis()获取标准时间可以通过system.currenttimemillis()方法获取,此方法不受时区影响,得到的结果是时间戳格式的。例如:1543105352845我们可以将... -
java里 currenttime_java 获取当前时间LocalDateTime currentTimeMillis java.util.Date
2021-02-26 12:27:10总结java里面关于获取当前时间的一些方法System.currentTimeMillis()获取标准时间可以通过System.currentTimeMillis()方法获取,此方法不受时区影响,得到的结果是时间戳格式的。例如:1543105352845我们可以将... -
java如何获取bios时间_获取Java中的BIOS时间 - java
2021-03-14 03:38:23时Java是否使用BIOS(基本输入/输出系统)日期时间我确实知道Java以格林尼治标准时间1970年1月1日00:00:00为基础以某种方式工作当前的日期和时间。但是到底如何呢?参考方案它询问操作系统,可能询问BIOS,也可能不... -
java joda 获取utc时间_Java获取时间与系统时间相差8小时终极解决方案
2021-02-27 22:14:01GMT时间UTC时间是时间标准时间(Universal Time Coordinated),UTC是根据原子钟来计算时间,误差非常小。UTC也是指零时区的时间,如果要表示其他时区的时间,这里要注意没有UTC+0800或者UTC+8这样的表示方式(至少Ja..... -
java方式获取网络时间(国家标准时间)
2017-07-07 12:04:16转:http://blog.csdn.net/catoop/article/details/50076879 ————————————————————————————————————————————因为经测试发现... * */ ... import java.i -
java当前时间获取
2020-01-16 21:11:29前言 接着源码阅读:new Date之旅,补充几种关于获取当前时间的方式。 Date 在Java中,获取当前日期最简单的方法之一就是...获取标准时间可以通过System.currentTimeMillis()方法获取,此方法不受时区影响,得到的结... -
java 获取UTC时间格式
2017-11-02 11:19:08/** ... * [获取UTC标准时] * [功能详细描述] * @return * @exception/throws [违例类型] [违例说明] * @see [类、类#方法、类#成员] */ private String getUTCDate() { String dateStr = -
java获取本周时间
2014-08-13 09:51:002019独角兽企业重金招聘Python工程师标准>>> ... -
java获取给定时区时间,中国标准时.格林威治时间
2015-06-04 16:02:15package testContainer; import java.text.DateFormat; import java.text.SimpleDateFormat;...import java.time.Instant;...import java.util.Calendar;...import java.util.Date;...import java.util.GregorianC -
gmt java date_关于日期:以Java获取GMT时间
2021-03-07 21:38:52在Java中,我想获取格林尼治标准时间的当前时间。我尝试了各种选择,例如:Date date = new Date();Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));date1 = calendar.getTime();但是日期... -
java获取当月1号 的时间chuo_java利用时间戳来获取UTC时间
2021-01-27 01:57:35由于项目原因,本人在做测试的时候发时间都是UTC时间,因此找了找解决方案,发现都是非常复杂,十几行代码实现此功能,其中主要都用在计算时间偏移量。...本方法只适用于采用北京时间为标准时间的地区... -
java 货物时区 时间差_Java获取时间与系统时间相差8小时终极解决方案
2021-03-09 06:15:58GMT时间UTC时间是时间标准时间(Universal Time Coordinated),UTC是根据原子钟来计算时间,误差非常小。UTC也是指零时区的时间,如果要表示其他时区的时间,这里要注意没有UTC+0800或者UTC+8这样的表示方式(至少Ja..... -
java获取本地时间格式
2014-11-05 18:25:002019独角兽企业重金招聘Python工程师标准>>> ... -
Java获取GMT(格林威治)时间
2020-03-13 10:19:56格林威治时间(GMT)是指格林威治所在地的标准时间 ,是北京时间减去8个小时后的时间。 1、以前是通过new Date().toGMTString()来获取,但是已过时; 2、现在通过以下方法获取: import java.text.DateFormat; ... -
28.Java获取系统时间
2017-08-07 00:46:002019独角兽企业重金招聘Python工程师标准>>> ... -
JAVA获取某个时区的时间
2016-04-07 17:31:41比如用java获取纽约的标准时间,代码如下: SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Calendar calendar = Calendar.getInstance(); sdf.setTimeZone(TimeZone.getTimeZone(... -
获取当前日期和时间java语句是_java如何获取当前日期和时间
2021-02-28 12:52:54本篇博客主要总结java里面关于获取当前时间的一些方法System.currentTimeMillis()获取标准时间可以通过System.currentTimeMillis()方法获取,此方法不受时区影响,得到的结果是时间戳格式的。例如:1543105352845... -
java获取当前时区的时间_<转>java获取当前时区的时间 - - ITeye博客
2021-02-12 18:00:35原来CST是美国中部标准时间改过之后的就可以了。下面是正确的程序import java.text.DateFormat;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import java.util.... -
java 8小时_Java获取时间与系统时间相差8小时终极解决方案
2021-02-12 15:57:12GMT时间UTC时间是时间标准时间(Universal Time Coordinated),UTC是根据原子钟来计算时间,误差非常小。UTC也是指零时区的时间,如果要表示其他时区的时间,这里要注意没有UTC+0800或者UTC+8这样的表示方式(至少Ja.....