‘壹’ 搞清编程中常见的CST、GMT、UTC时间问题
看一下java的当前时间:
在浏览器上输出javascript的当前时间:
对比一下,发现java的显示用了CST时间,javascprit用了GMT时间,那么二者有什么区别呢
GMT(Greenwich Mean Time)代表格林尼治标准时间,这个大家普通都知道,
而CST却同时可以代表如下 4 个不同的时区,与GMT时间的关系如下:
有时还会看到 2020-09-25T 05:35:00.968 Z 这种时间,其实这种时间格式就是一个标准时间(其中T是分隔符,Z表示这个时间是标准时间),如果转化为当地时间(北京),要按照时区划分+8小时。
‘贰’ C#中怎么实现本地时间与UTC时间的相互转换
时间戳就是如1377216000000 这种格式,在mysql数据库中会经常用到把时间转换成时间戳或把时间戳转换成日期格式了,下面是时间戳操作转换方法:
一、原理
时间戳的原理是把时间格式转为十进制格式,这样就方便时间的计算
如: 2013年08月23日 转化后是 1377216000000
二、步骤
1、创建 DateUtilsl类。
代码如下:
importjava.text.ParseException;
importjava.text.SimpleDateFormat;
importjava.util.Date;
/*
* @author Msquirrel
*/
public class DateUtils {
privateSimpleDateFormat sf = null;
/*获取系统时间 格式为:"yyyy/MM/dd "*/
public static String getCurrentDate() {
Date d = newDate();
sf = newSimpleDateFormat("yyyy年MM月dd日");
returnsf.format(d);
}
/*时间戳转换成字符窜*/
public static String getDateToString(long time) {
Date d = newDate(time);
sf = newSimpleDateFormat("yyyy年MM月dd日");
returnsf.format(d);
}
/*将字符串转为时间戳*/
public static long getStringToDate(String time) {
sdf = newSimpleDateFormat("yyyy年MM月dd日");
Date date = newDate();
try{
date = sdf.parse(time);
} catch(ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
returndate.getTime();
}
2、在对应使用的地方调用。
代码如下:
DateUtils.getCurrentDate(); //获取系统当前时间
DateUtils.getDateToString(时间戳); //时间戳转为时间格式
DateUtils.getStringToDate("时间格式");//时间格式转为时间戳.
‘叁’ 在java里面,如何得到UTC时间, 时间格式为:Tue Oct 12 00:00:00 UTC 0800 2010
Calendar gc = GregorianCalendar.getInstance();
cal.add(java.util.Calendar.MILLISECOND, -(zoneOffset + dstOffset));
//之后调用cal.get(int x)或cal.getTimeInMillis()方法所取得的时间即是UTC标准时间。
System.out.println("UTC:"+new Date(cal.getTimeInMillis()));
赠送其它时间方法,总有一款适合您
public static void main(String[] args) {
SimpleDateFormat foo = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
System.out.println("foo:"+foo.format(new Date()));
Calendar gc = GregorianCalendar.getInstance();
System.out.println("gc.getTime():"+gc.getTime());
System.out.println("gc.getTimeInMillis():"+new Date(gc.getTimeInMillis()));
//当前系统默认时区的时间:
Calendar calendar=new GregorianCalendar();
System.out.print("时区:"+calendar.getTimeZone().getID()+" ");
System.out.println("时间:"+calendar.get(Calendar.HOUR_OF_DAY)+":"+calendar.get(Calendar.MINUTE));
//美国洛杉矶时区
TimeZone tz=TimeZone.getTimeZone("America/Los_Angeles");
//时区转换
calendar.setTimeZone(tz);
System.out.print("时区:"+calendar.getTimeZone().getID()+" ");
System.out.println("时间:"+calendar.get(Calendar.HOUR_OF_DAY)+":"+calendar.get(Calendar.MINUTE));
Date time=new Date();
//1、取得本地时间:
java.util.Calendar cal = java.util.Calendar.getInstance();
//2、取得时间偏移量:
int zoneOffset = cal.get(java.util.Calendar.ZONE_OFFSET);
//3、取得夏令时差:
int dstOffset = cal.get(java.util.Calendar.DST_OFFSET);
//4、从本地时间里扣除这些差量,即可以取得UTC时间:
cal.add(java.util.Calendar.MILLISECOND, -(zoneOffset + dstOffset));
//之后调用cal.get(int x)或cal.getTimeInMillis()方法所取得的时间即是UTC标准时间。
System.out.println("UTC:"+new Date(cal.getTimeInMillis()));
Calendar calendar1 = Calendar.getInstance();
TimeZone tztz = TimeZone.getTimeZone("GMT");
calendar1.setTimeZone(tztz);
System.out.println(calendar.getTime());
System.out.println(calendar.getTimeInMillis());
}
运算结果是Tue Oct 19 16:54:57 CST 2010 符合你的要求
只是以毫秒来算的
‘肆’ 在java里面,如何得到UTC时间, 时间格式为:Tue Oct 12 00:00:00 UTC 0800 2010
public class StringToDate {
public static void main(String []args){
String myString="2011-09-18 11:20:30";
Date myDate=null;
DateFormat df = DateFormat.getDateInstance();
//设置时间格式
SimpleDateFormat myFormDate = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
try {
myDate = df.parse(myString);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(myDate);
}
}
结果:Sun Sep 18 00:00:00 CST 2011
‘伍’ JAVA UTC时间格式转换
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss);
sdf.format(new Data());
‘陆’ 关于时间换算的JAVA题目
因为题目要求是输入0到2359之间的数 并没有指出十位和个位组合数一定是分钟小于60的形式 所以要多考虑 代 码如下
import java.util.Scanner;
class A{
public static void main(String args[]){
Scanner sd=new Scanner(System.in);
int BJT;
BJT=sd.nextInt();
int y=BJT-(int)(BJT/100)*100;
if(BJT>=0&&BJT<=60){
System.out.println(BJT+1600);
}else if(BJT<=99){
System.out.println("输入有误");
}if(BJT>99&&BJT<800){
if(y>=0&&y<=60){
System.out.println(BJT+1600);
}else System.out.println("输入有误");
}
else if(BJT>=800&&BJT<1800){
if(y>=0&&y<=60){
System.out.println("0"+(BJT-800));
}else System.out.println("输入有误");
}else if(BJT>=1800&&BJT<=2359){
if(y>=0&&y<=60){
System.out.println(BJT-800);
}else System.out.println("输入有误");
}
}
}
‘柒’ UTC时间
UTC是协调世界时(Universal Time Coordinated)英文缩写,是由国际无线电咨询委员会规定和推荐,并由国际时间局(BIH)负责保持的以秒为基础的时间标度。UTC相当于本初子午线(即经度0度)上的平均太阳时,过去曾用格林威治平均时(GMT)来表示.北京时间比UTC时间早8小时,以1999年1月1日0000UTC为例,UTC时间是零点,北京时间为1999年1月1日早上8点整。
整个地球分为二十四时区,每个时区都有自己的本地时间。在国际无线电通信场合,为了统一起见,使用一个统一的时间,称为通用协调时(UTC, Universal Time Coordinated)。UTC与格林尼治平均时(GMT, Greenwich Mean Time)一样,都与英国伦敦的本地时相同。
‘捌’ java utc时间转本地时间
JAVA中将UTC时间转换为本地时间的方法,其他的时区转换与此类似。
public static String utc2Local(String utcTime, String utcTimePatten,
String localTimePatten) {
SimpleDateFormat utcFormater = new SimpleDateFormat(utcTimePatten);
utcFormater.setTimeZone(TimeZone.getTimeZone("UTC"));//时区定义并进行时间获取
Date gpsUTCDate = null;
try {
gpsUTCDate = utcFormater.parse(utcTime);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat localFormater = new SimpleDateFormat(localTimePatten);
localFormater.setTimeZone(TimeZone.getDefault());
String localTime = localFormater.format(gpsUTCDate.getTime());
return localTime;
}
‘玖’ java的date怎么转换成utc
使用 java.text.SimpleDateFormat指定格式
~~~~~~~~~~~~~~