-
java中的Date和时区
2019-12-10 18:56:47Java中的Date和时区 Date date = new Date(); System.out.println(date); System.out.println(date.getTime()); // 输出 Tue Dec 10 18:44:24 CST 2019 1575974664352 Date中保存的是什么? 查看源码可以...Date本身没有时区概念
查看源码可以知道, Date对象中存储的是一个long型变量
这个变量的值为自1997-01-01 00:00:00(GMT)至Date对象记录时刻所经过的毫秒数
可以通过getTime()方法,获取这个变量值,且这个变量值和时区没有关系
全球任意地点同时执行new Date().getTime()获取到的值相同Date源码
private transient long fastTime; /** * Allocates a <code>Date</code> object and initializes it so that * it represents the time at which it was allocated, measured to the * nearest millisecond. * * @see java.lang.System#currentTimeMillis() */ public Date() { this(System.currentTimeMillis()); } /** * Allocates a <code>Date</code> object and initializes it to * represent the specified number of milliseconds since the * standard base time known as "the epoch", namely January 1, * 1970, 00:00:00 GMT. * * @param date the milliseconds since January 1, 1970, 00:00:00 GMT. * @see java.lang.System#currentTimeMillis() */ public Date(long date) { fastTime = date; } /** * Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT * represented by this <tt>Date</tt> object. * * @return the number of milliseconds since January 1, 1970, 00:00:00 GMT * represented by this date. */ public long getTime() { return getTimeImpl(); } private final long getTimeImpl() { if (cdate != null && !cdate.isNormalized()) { normalize(); } return fastTime; } /** * Converts this <code>Date</code> object to a <code>String</code> * of the form: * <blockquote><pre> * dow mon dd hh:mm:ss zzz yyyy</pre></blockquote> * where:<ul> * <li><tt>dow</tt> is the day of the week (<tt>Sun, Mon, Tue, Wed, * Thu, Fri, Sat</tt>). * <li><tt>mon</tt> is the month (<tt>Jan, Feb, Mar, Apr, May, Jun, * Jul, Aug, Sep, Oct, Nov, Dec</tt>). * <li><tt>dd</tt> is the day of the month (<tt>01</tt> through * <tt>31</tt>), as two decimal digits. * <li><tt>hh</tt> is the hour of the day (<tt>00</tt> through * <tt>23</tt>), as two decimal digits. * <li><tt>mm</tt> is the minute within the hour (<tt>00</tt> through * <tt>59</tt>), as two decimal digits. * <li><tt>ss</tt> is the second within the minute (<tt>00</tt> through * <tt>61</tt>, as two decimal digits. * <li><tt>zzz</tt> is the time zone (and may reflect daylight saving * time). Standard time zone abbreviations include those * recognized by the method <tt>parse</tt>. If time zone * information is not available, then <tt>zzz</tt> is empty - * that is, it consists of no characters at all. * <li><tt>yyyy</tt> is the year, as four decimal digits. * </ul> * * @return a string representation of this date. * @see java.util.Date#toLocaleString() * @see java.util.Date#toGMTString() */ public String toString() { // "EEE MMM dd HH:mm:ss zzz yyyy"; BaseCalendar.Date date = normalize(); StringBuilder sb = new StringBuilder(28); int index = date.getDayOfWeek(); if (index == BaseCalendar.SUNDAY) { index = 8; } convertToAbbr(sb, wtb[index]).append(' '); // EEE convertToAbbr(sb, wtb[date.getMonth() - 1 + 2 + 7]).append(' '); // MMM CalendarUtils.sprintf0d(sb, date.getDayOfMonth(), 2).append(' '); // dd CalendarUtils.sprintf0d(sb, date.getHours(), 2).append(':'); // HH CalendarUtils.sprintf0d(sb, date.getMinutes(), 2).append(':'); // mm CalendarUtils.sprintf0d(sb, date.getSeconds(), 2).append(' '); // ss // 注意: 这里涉及到时区 TimeZone zi = date.getZone(); if (zi != null) { sb.append(zi.getDisplayName(date.isDaylightTime(), TimeZone.SHORT, Locale.US)); // zzz } else { sb.append("GMT"); } sb.append(' ').append(date.getYear()); // yyyy return sb.toString(); }
getTime()获取毫秒数,获取到的毫秒数和格式化后时间的关系
这个变量的值为自1997-01-01 00:00:00(GMT)至Date对象记录时刻所经过的毫秒数
这个毫秒数和格式化后时间是模型和视图的关系, 时区(TimeZone)决定了同一模型展示成什么样的视图(格式化Date)Date date = new Date(); System.out.println(date); System.out.println(date.getTime()); // 输出 Tue Dec 10 18:44:24 CST 2019 1575974664352
格式化Date对象成字符串, 涉及时区
不管是调用Date对象的toString方法, 还是使用SimpleDateFormat的format方法去格式化Date对象,或者使用parse解析字符串成Date对象都会涉及到时区,
也就是说Date对象没有时区概念, 但是格式化Date对象, 或者解析字符串成Date对象时, 是有时区概念的toString
Date date = new Date(); // 默认是系统时区 System.out.println(date); // 修改默认时区 TimeZone.setDefault(TimeZone.getTimeZone("GMT")); System.out.println(date); // 输出 Tue Dec 10 19:03:46 CST 2019 Tue Dec 10 11:03:46 GMT 2019
SimpleDateFormat
Date date = new Date(); // 默认是系统时区 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println(dateFormat.format(date)); // 设置时区 dateFormat.setTimeZone(TimeZone.getTimeZone("GMT+1:00")); System.out.println(dateFormat.format(date)); // 输出 2019-12-10 19:06:31 2019-12-10 12:06:31
解析字符串成Date对象, 涉及时区
将同一个时间字符串按照不同的时区来解析, 得到的Date对象值不一样
很好理解: 东八区8点当然和0时区8点不一样String dateStr = "2019-12-10 08:00:00"; // 默认是系统时区 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date1 = dateFormat.parse(dateStr); System.out.println(date1.getTime()); // 设置时区 dateFormat.setTimeZone(TimeZone.getTimeZone("GMT+1:00")); Date date2 = dateFormat.parse(dateStr); System.out.println(date2.getTime()); // 输出 1575936000000 1575961200000
将本地的时间字符串转换成另一时区的时间字符串
String dateStr = "2019-12-10 08:00:00"; // 按照本地时区解析字符串成Date SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date1 = dateFormat.parse(dateStr); // 使用目标时区格式化Date dateFormat.setTimeZone(TimeZone.getTimeZone("GMT+9:00")); System.out.println(dateFormat.format(date1)); // 输出 2019-12-10 09:00:00
-
java中的Date数据类型对应mysql中的数据类型
2020-03-14 19:32:35 -
JAVA中的Date类
2020-11-17 20:44:08JAVA中如果想获取当前的日期时间可以直接通过java.util.Date类来实现,此类的常用操作如下表所示 例子: 获取当前日期时间 package DateDemo; import java.util.Date; public class Test { public static void ...JAVA中如果想获取当前的日期时间可以直接通过java.util.Date类来实现,此类的常用操作如下表所示
例子: 获取当前日期时间
package DateDemo; import java.util.Date; public class Test { public static void main(String[] args) throws Exception { Date date = new Date();//实例化对象 System.out.println(date); //直接输出对象 } } //运行结果 Tue Nov 17 20:40:07 CST 2020
本段代码直接利用Date类提供的无参构造方法实例化了Date类对象,此时Date对象将会保存当前的日期时间.
例2 Date与long之间转换处理
package DateDemo; import java.util.Date; public class Test { public static void main(String[] args) { Date date = new Date(); //实例化Date类对象 long current = date.getTime(); //获得当前时间戳 current += 864000 * 1000; //10天的秒数 System.out.println(new Date(current)); } } //运行结果 Fri Nov 27 20:46:22 CST 2020
Date类对象保存的时间戳是以毫秒的形式记录的当前的日期和时间,所以在上述代码中将当前日期时间戳取出并加上10天的毫秒数就可以获取十天以后的日期.
-
sql中的date数据到java中的date类型转换
2014-01-02 17:58:11代码中设置的超时机制没有起效,发现是从数据库中取的sql中的date数据到java中的date的值变化了,原因如下: java代码写的sql语句取到的sql值的类型为Java.sql.Date,该类型在取到值后,会默认的将时分秒去掉,只...代码中设置的超时机制没有起效,发现是从数据库中取的sql中的date数据到java中的date的值变化了,原因如下:
java代码写的sql语句取到的sql值的类型为Java.sql.Date,该类型在取到值后,会默认的将时分秒去掉,只保留日期,再代码中做的date转换结果就错了。所以,从数据库中取时间的时候,需要先做类型转换,在sql语句中将时间转换为string类型的取出来
to_char(TF_START_TIME, 'yyyy-mm-dd hh24:mi:ss') "stateTime"
然后在代码中,用java.util.Date类型去转换。
static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat df = new SimpleDateFormat(DEFAULT_DATE_FORMAT );
Date result= df.parse(value);这样才能保证值的正确性。
附上一段其他兄弟写的两个类型之间互相转换的东西作为补充:
java.sql.Date转为java.util.Date
java.sql.Date date=new java.sql.Date();
java.util.Date d=new java.util.Date (date.getTime());java.util.Date转为java.sql.Date
java.util.Date utilDate=new Date();
java.sql.Date sqlDate=new java.sql.Date(utilDate.getTime());
java.sql.Time sTime=new java.sql.Time(utilDate.getTime());
java.sql.Timestamp stp=new java.sql.Timestamp(utilDate.getTime()); -
Java中的Date和时区转换
2017-08-25 14:42:261.Date中保存的是什么在java中,只要我们执行Date date = new Date();就可以得到当前时间。如:Date date = new Date(); System.out.println(date);输出结果是:Thu Aug 24 10:15:29 CST 2017也就是我执行上述代码的... -
Java中的Date类与SQL中的Date类的相互转换
2019-10-22 15:34:39Java中的时间类型 java.sql包下给出三个与数据库相关的日期时间类型,分别是: Date:表示日期,只有年月日,没有时分秒。会丢失时间; Time:表示时间,只有时分秒,没有年月日。会丢失日期; Timestamp:表示... -
Java中的Date详解
2019-04-25 11:01:51* Date:表示特定的瞬间,精确到毫秒。 * * 构造方法: * Date():根据当前的默认毫秒值创建日期对象 * Date(long date):根据给定的毫秒值创建日期对象 */ public class DateDemo { public static void ... -
使用Java中的Date和Calendar类
2014-09-30 23:08:45使用Java中的Date和Calendar类 Java 语言的Calendar(日历),Date(日期), 和DateFormat(日期格式)组成了Java标准的一个基本但是非常重要的部分. 日期是商业逻辑计算一个关键的部分. 所有的开发者都应该能够计算未来的... -
java中的Date和TimeStamp类的区别
2018-04-03 17:03:001 使用Date包为java.util.Date ,Date表示特定的瞬间,精确到毫秒。 2 Timestamp此类型由 java.util.Date 和单独的毫微秒值组成。...数据库中是TIMESTAMP的,对应java的Timestamp,使用Date就会报错; ... -
mysql中的datetime类型和java中的date类型转换
2018-05-25 11:22:00原问题网址:https://ask.csdn.net/questions/230514原问题答案:首先java.sql.Date是java.uti.Date的子类,但是java.sql.Date是sql相关的日期类型,我们用纯JDBC写数据库连接、查询时所用的日期就是这个类型。... -
后端开发 数据库 关于 mysql的datetime 和 java中的 Date的转换
2020-07-01 10:48:48java date 转mysql dateTime ...注意 从数据库查出来的date类型 是不可以转为String类型的; mysql中查询语句date类型的字段可以先转为字符串类型 再作比较 例:select * from 表名 where date_form -
Java中的Date类及其用法
2019-03-08 17:39:39import java.util.Date; /** * java.util.Date * Date的每一个实例用于表示一个确切的时间点。 * 内部维护了一个long值,该值记录的是从: * 1970年1月1日00:00:00到表示的时间点之间所经历的毫秒值。 * ... -
Java中的Date和Calendar的常用用法
2016-08-04 18:45:52在java中用到的最多的时间类莫过于 java.util.Date了, 由于Date类中将getYear(),getMonth()等获取年、月、日的方法都废弃了, 所以要借助于Calendar来获取年、月、日、周等比较常用的日期格式 注意:以下代码均... -
java中的date和sql的datetime
2019-11-28 17:23:23转载链接:https://blog.csdn.net/qq_40915081/article/details/80298176 -
Java中的Date类和LocalDate类
2018-05-09 14:37:56Date:时间点LocalDate:日期Date :Date today =new...{略:Date(年月日):年是从1900开始多少年,月份减一,日期直接插入}LocalDate:创建对象不用构造器,用静态工厂方法(factory method)import java.util.*; ... -
关于JavaScript中的date和java中的date差14小时问题
2016-11-14 11:25:56今天遇到一个问题,在java中获取的时间传到前台页面, 原时间是这样的:2016-11-10 15:29:11, 传到前台来是这样的:Thu Nov 10 15:29:11 CST 2016, 在js中用getDate(),结果是11,getHours(),结果是5,也就是11号... -
关于Java中的date数据类型如何保存到Oracle中
2018-08-07 08:12:02使用java中提供的Timestamp时间戳来替代date类型即可保存...也是将Bean中的Date类型数据转化为Timestamp类型再向数据库进行保存操作。 查询的时候mybatis也同样可以自动进行类型转化。 今天用Db... -
java 中的date类型和oracle中的date类型间的转换
2012-09-13 19:39:25//从oracle数据库中读出date类型的数据并转换成java的data类型 SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// HH24小时制,hh是12小时制 mSendTime = (Date) sd.parse(rs.getString(2),new... -
Java中的Date类(二):Date parse——从字符串转换为指定格式的Date
2019-03-08 17:47:20package day03; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date;... * 将给定的字符串按照SimpleDateFormat指定的日期格式解析为一个Date对象。... -
Java中的Date类转为mysql中的timestamp类
2018-07-16 13:08:30private static Date calendarToData(int year, int month, int day ,int hourOfday,int minute,int second) { Calendar calendar = Calendar.getInstance(); calendar.set(year, month - 1, day... -
Java中的Date类(一):SimpleDateFormat——String与Date的相互转换
2019-03-08 17:42:01package day03; import java.text.SimpleDateFormat; import java.util.Date;... * 根据一个给定的日期格式将String与Date相互转换。 * @author kaixu * */ public class SimpleDateFormatDemo1 ... -
关于Java中的Date时间显示问题。
2017-06-19 09:02:27代码如下,就是String日期类转Date类后的打印,12点的时候显示的不是12点,而是0点,而13点显示的13点,如果说是12小时制,那么13—23都能显示是什么问题? ``` public class CalendarTest1 { public static void... -
Java中的Date Time 与SQL Server 2005里的Datetime 之间的交互
2008-05-20 15:34:00PrefaceEnvironment:Platform: Windows XPLanguage: Java 1.5IDE: MyEclipse 6.0.1Database: SQL Server 2005 Enterprise enIntroduction本文主要讲述Java中的Date Time 与SQL Server 2005里的Datetime 如何进行交互... -
java中的Date转换为sql中个的date
2012-11-06 16:16:33SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); /////////////////HH为24计时格式 String dateStringToParse = "2007-7-12 14:00:00"; try{ ... java.util.D -
sql数据库与java中的date类型
2011-10-16 10:07:18在SQL Server数据库中是不支持java中的java.util.Date的,只支持java.sql.Date()。所以在有些程序中会显示不支持Date的原因 ……%