
时间戳通常是”yyyyMMddHHmmss“的,举例:
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String str = sdfformat(date);
输出结果:20150704173752。
备注:时间戳中的时间显示格式可以根据实际情况设置即可。
import javatextSimpleDateFormat;import javautilDate;
public class Timedemo {
public static void main(String[] args)
{
long time=SystemcurrentTimeMillis();
Date date=new Date(time);
String mat="yyyy-MM-dd";
String ma="yyyyMMdd";
SimpleDateFormat format=new SimpleDateFormat(mat);
SimpleDateFormat forma=new SimpleDateFormat(ma);
String nowdate=formatformat(date);
String nwdate=formaformat(date);
int x=IntegerparseInt(nwdate);
Systemoutprintln(nowdate);
Systemoutprintln(nwdate);
Systemoutprintln(x);
}
}
经过测试满足以上条件 希望对你有帮助
如果你要获取的是Internet时间,可以使用NTP服务。
NTP概念简介
Network Time Protocol(NTP)是用来使计算机时间同步化的一种协议,它可以使计算机对其服务器或时钟源(如石英钟,GPS等等)做同步化,它可以提供高精准度的时间校正(LAN上与标准间差小于1毫秒,WAN上几十毫秒),且可介由加密确认的方式来防止恶毒的协议攻击。
java实现:
import javaioInputStream;import javanetSocket;
public class TimeUtil {
public static final int DEFAULT_PORT = 37;//NTP服务器端口
public static final String DEFAULT_HOST = "time-nwnistgov";//NTP服务器地址
private TimeUtil() {
};
public static long currentTimeMillis(Boolean sync) {
if (sync != null && syncbooleanValue() != true)
return SystemcurrentTimeMillis();
try {
return syncCurrentTime();
} catch (Exception e) {
return SystemcurrentTimeMillis();
}
}
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 = TimeZonegetTimeZone("GMT"); Calendar epoch1900 =
CalendargetInstance(gmt); epoch1900set(1900, 01, 01, 00, 00, 00);
long epoch1900ms = epoch1900getTime()getTime(); Calendar epoch1970
= CalendargetInstance(gmt); epoch1970set(1970, 01, 01, 00, 00, 00);
long epoch1970ms = epoch1970getTime()getTime();
long differenceInMS = epoch1970ms - epoch1900ms; long
differenceBetweenEpochs = differenceInMS/1000;
/
InputStream raw = null;
try {
Socket theSocket = new Socket(DEFAULT_HOST, DEFAULT_PORT);
raw = theSocketgetInputStream();
long secondsSince1900 = 0;
for (int i = 0; i < 4; i++) {
secondsSince1900 = (secondsSince1900 << 8) | rawread();
}
if (raw != null)
rawclose();
long secondsSince1970 = secondsSince1900 - differenceBetweenEpochs;
long msSince1970 = secondsSince1970 1000;
return msSince1970;
} catch (Exception e) {
throw new Exception(e);
}
}
}
中国大概能用的NTP时间服务器
server 133100118 prefer
server 2107214544
server 20311718036 //程序中所用的
server 131107110
server timeasiaapplecom
server 642369653
server 1301491721
server 669268246
server >
你这个就是当前时间和你说的电脑显示时间两个是一样的= ="
CalendargetInstance()getTime()<----系统时间
抓网路时间的方法 大陆区请自行换抓取网址
/
网路时间
@return
/
public String date() {
String date = null;
try {
URL url = new URL(">
以上就是关于java 如何获取当前时间的时间戳全部的内容,包括:java 如何获取当前时间的时间戳、java 获取当天日期,并将日期转化成数值。、java获取网络当前时间等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)