simpledateformat

Java中时间与时间戳的转换

浪子不回头ぞ 提交于 2020-02-08 00:27:33
Timestamp就是时间戳,主要用于数据库中,该类在java.sql下。如果在数据库中用Date类,只能到某一天,而时间戳可以到秒,比Date要精确,通常用于防止数据脏读现象。 代码: package TimeAndStamp; import java.util.Arrays; import java.util.List; import java.text.SimpleDateFormat; import java.util.Date; public class test { /* * 将时间转换为时间戳 */ public static String dateToStamp(String time){ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String stamp = ""; if (!"".equals(time)) {//时间不为空 try { stamp = String.valueOf(sdf.parse(time).getTime()/1000); } catch (Exception e) { System.out.println("参数为空!"); } }else { //时间为空 long current_time = System

玩转时间操作 + 面试题

我的梦境 提交于 2020-02-06 10:01:19
玩转时间操作 + 面试题 在 JDK 8 之前,Java 语言为我们提供了两个类用于操作时间,它们分别是:java.util.Date 和 java.util.Calendar,但在 JDK 8 的时候为了解决旧时间操作类的一些缺陷,提供了几个新的类,用于操作时间和日期,它们分别是:LocalTime、LocalDateTime、Instant,都位于 java.time 包下。 时间的操作在我们日常的开发中经常见到,比如,业务数据都要记录创建时间和修改时间,并要把这些时间格式化之后显示到前端页面,再比如我们需要计算业务数据的时间间隔等,都离不开对时间的操作,那如何正确而优雅地使用时间?这就是我们接下来要讨论的话题。 时间基础知识科普 格林威治时间 格林威治(又译格林尼治)是英国伦敦南郊原格林威治天文台的所在地,它是世界计算时间和地球经度的起点,国际经度会议 1884 年在美国华盛顿召开,会上通过协议,以经过格林威治天文台的经线为零度经线(即本初子午线),作为地球经度的起点,并以格林威治为“世界时区”的起点。 格林威治时间和北京时间的关系 格林威治时间被定义为世界时间,就是 0 时区,北京是东八区。也就是说格林威治时间的 1 日 0 点,对应到北京的时间就是 1 日 8 点。 时间戳 时间戳是指格林威治时间 1970-01-01 00:00:00(北京时间 1970-01-01

查询出的时间类型的数据是一串数字, 怎么转化为时间格式格式

China☆狼群 提交于 2020-02-05 07:59:01
数据库查询出来的sysCreateDate的数据是一串数字1478065615000, 怎么转换为"2016-11-11 05:36:56"的形式呢? 怎么才能转换为可以认识的数据类型啊? 解决方法: 使用 @JsonFormat注解 @JsonFormat注解是一个时间格式化注解,比如我们存储在mysql中的数据是date类型的,当我们读取出来封装在实体类中的时候,就会变成英文时间格式,而不是yyyy-MM-dd HH:mm:ss这样的中文时间,因此我们需要用到JsonFormat注解来格式化我们的时间。 JsonFormat注解是jackson包里面的一个注解,因此在使用的时候需要引入fasterxml maven的jar包,如下所示。 <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.2</version> </dependency> 引入fasterxml maven jar包之后,就可以在实体类属性上面使用@JsonFormat注解了,要注意的是,它只会在类似@ResponseBody返回json数据的时候,才会返回格式化的yyyy-MM-dd HH:mm:ss时间,你直接使用System.out

SimpleDateFormat setTimeZone not working

橙三吉。 提交于 2020-02-03 08:23:18
问题 I am using the below code to cast time as UTC which is working import java.text.SimpleDateFormat; import static java.util.Calendar.* def dt = "2018-03-19T06:00:00+01:00" def format = "yyyy-MM-dd'T'HH:mm:ssX" TimeZone tz = TimeZone.getDefault(); //getting up local time zone TimeZone.setDefault(TimeZone.getTimeZone("UTC")); SimpleDateFormat sdf = new SimpleDateFormat(format); Date d = sdf.parse(dt); TimeZone.setDefault(tz); println d //output: 2018-03-19T05:00:00Z println d.toTimestamp(); /

20200202,千年难遇啊!

◇◆丶佛笑我妖孽 提交于 2020-02-02 17:57:24
本文收录在Java技术栈Github,欢迎Star: https://github.com/javastacks/javastack 已经在家好几天没出门了,是不是都憋疯了?今天又是个特殊的日子,我来给大家分享一个节日小知识。 20200202 为什么说是千万难遇的一天? 因为今天是:世界完全对称日,很少遇见,看起来和回文数差不多,如: 2020 倒过来就是 0202,拼在一起就是:20200202 这个节日是否存在科学意义还无从考证,但却是十分有意思的一天,一千年也就那么十来个。 今天是农历正月初九,被大家寓意为长长久久,又因 “2020” 谐音 “爱你爱你”,又被不少人称之为千年一遇的 “最佳领证日”! 这不,很多新人们都相继预约在 20200202 这天结婚领证,年前就已经排满了,但最近些天,不巧,这个事吧,我估计悬了。。 既然世界完全对称日很少见,那都有哪些呢? 下面,栈长用 Java 程序打出了 21 世纪所有的世界完全对称日,也就 12 个。 import java.text.SimpleDateFormat; import java.util.Calendar; /** * @author 栈长 * @from 微信公众号:Java技术栈 */ public class SymmetryDay { public static void main(String[]

Month without leading zeros in Android

无人久伴 提交于 2020-02-01 01:37:50
问题 Is there a way to format a Month without leading zeros in Java/Android? I got this: mTitleText.setText(String.format("Pick Date Time: %tm/%te/%tY %tk:%02d", mTime,mTime,mTime,mTime,minute)); And it returns 02/12/2012 13:23 when I want it to return 2/12/2012 13:23. 回答1: For those interested in avoiding the lengthy JavaDocs: Date mTime = new Date(); String text = new SimpleDateFormat("M/d/yyyy hh:mm").format(mTime); Using M instead of MM and d instead of dd will render the day of the month and

Java学习笔记 (六)

风格不统一 提交于 2020-01-30 18:18:11
Java 日期 日期 Date  涉及到的一些方法,语法 1 // 获取当前时间 2 Date now = new Date(); 3 4 // 获取 1970年 1月 1日 早上 8.00 的时间 5 Date ThatTime = new Date(0) 6 7 // 获取 当前时间从 1970 那时起到现在经历的毫秒数,两种方法 8 // 1. getTime() 方法,得到一个 long类型的整数 9 now.getTime() 10 11 // 2. System.currentTimeMillis()方法 12 System.currentTimeMillis() 13 14 // 设置时间, 假设设置时间为 2000年12月12日 08:08:55 15 Date setTime = new Date(2000, 12, 12 , 08, 08, 55)  日期与字符串间的关系   SimpleDateFormat 为 日期格式化类 Y 代表年 M 代表月 d 代表日 H 代表24进制的小时 h 代表12进制的小时 m 代表分钟 s 代表秒 S 代表毫秒   下面这个函数可以将日期格式化成字符串 1 public String Format(Date date){ 2 SimpleDateFormat sdf = new SimpleDateFormat(""yyyy

Java SE入门(十六)——高级API

放肆的年华 提交于 2020-01-30 12:41:34
Java SE入门(十六)——高级API   iwehdio的博客园:https://www.cnblogs.com/iwehdio/ 1.、Object类 是类层次结构的根类,每个类都使用Object作为超类 任何类都默认继承了Object类。 getClass() 返回一个字节码对象。 String toString() 方法: 返回对象的字符串表示。 输出一个对象就是默认输出这个对象的 toString() 方法。 输出格式: 包名 . 类名 @ 对象的十六进制内存地址。 一般要重写更有意义的 toString() 方法。 Eclipse快速生成 toString() 方法:右键 > Source > Generate toString()。 boolean equals(Object obj) 方法: 使用 == 来比较两个对象是否相等。基本类型比较值,引用类型比较地址。 一般要重写更有意义的 toString() 方法。比如字符串类重写的 equals 方法。 通过比较传入参数是否与对象是同一个类下的对象 if(this.getClass()==o.getClass()) 。 Eclipse快速生成 equals() 方法:右键 > Source > Generate equals() 。 2、System类 包含一些有用的类字段(静态修饰的成员变量)和方法,不能被实例化

SimpleDateFormat throws ParseException With Error Offset 0

僤鯓⒐⒋嵵緔 提交于 2020-01-30 06:33:24
问题 What's wrong with the following code? It throws a ParseException with error offset 0. final DateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy"); df.parse("Thu Jan 23 14:24:47 2014"); 回答1: If you don't specify a Locale to the formatter when you construct it, it uses your default Locale which apparently doesn't spell days and months in English. So specify one to the formatter that does. final DateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy", Locale.UK); 回答2: Is your

SimpleDateFormat throws ParseException With Error Offset 0

半城伤御伤魂 提交于 2020-01-30 06:33:06
问题 What's wrong with the following code? It throws a ParseException with error offset 0. final DateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy"); df.parse("Thu Jan 23 14:24:47 2014"); 回答1: If you don't specify a Locale to the formatter when you construct it, it uses your default Locale which apparently doesn't spell days and months in English. So specify one to the formatter that does. final DateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy", Locale.UK); 回答2: Is your