simpledateformat

聊聊 Java 中日期的几种常见操作 —— 取值、转换、加减、比较

亡梦爱人 提交于 2019-12-30 05:04:31
Java 的开发过程中免不了与 Date 类型纠缠,准备总结一下项目经常使用的日期相关操作,JDK 版本 1.7,如果能够帮助大家节约那么几分钟起身活动一下,去泡杯咖啡,便是极好的,嘿嘿。当然,我只提供了可行的解决方案,并不保证是最佳实践,欢迎讨论。 1. 日期取值 在旧版本 JDK 的时代,有不少代码中日期取值利用了 java.util.Date 类,但是由于 Date 类不便于实现国际化,其实从 JDK1.1 开始,就更推荐使用 java.util.Calendar 类进行时间和日期方面的处理。这里便不介绍 Date 类的操作,让我们直奔主题吧,如何利用 Calendar 类取得现在的日期时间。 由于 Calendar 的构造器方法被 protected 修饰,所以我们会通过 API 中提供的 getInstance 方法来创建 Calendar 对象。 1 //有多个重载方法创建 Calendar 对象 2 Calendar now = Calendar.getInstance(); //默认 3 //指定时区和地区,也可以只输入其中一个参数 4 Calendar now = Calendar.getInstance(timeZone, locale); 然后我们就可以通过该对象取得当前的各种时间参数了。 int year = now.get(Calendar.YEAR);

Java知多少(77)日期和时间类

旧时模样 提交于 2019-12-29 20:26:22
Java 的日期和时间类位于 java.util 包中。利用日期时间类提供的方法,可以获取当前的日期和时间,创建日期和时间参数,计算和比较时间。 Date 类 Date 类是 Java 中的日期时间类,其构造方法比较多,下面是常用的两个: Date():使用当前的日期和时间初始化一个对象。 Date(long millisec):从1970年01月01日00时(格林威治时间)开始以毫秒计算时间,计算 millisec 毫秒。如果运行 Java 程序的本地时区是北京时区(与格林威治时间相差 8 小时),Date dt1=new Date(1000);,那么对象 dt1 就是1970年01月01日08时00分01秒。 请看一个显示日期时间的例子: 1 import java.util.Date; 2 public class Demo{ 3 public static void main(String args[]){ 4 Date da=new Date(); //创建时间对象 5 System.out.println(da); //显示时间和日期 6 long msec=da.getTime(); 7 System.out.println("从1970年1月1日0时到现在共有:" + msec + "毫秒"); 8 } 9 } 运行结果: Mon Feb 05 22:50:05

Java定时任务调度工具Timer

牧云@^-^@ 提交于 2019-12-29 10:26:32
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 前言: 首先我们先明确一下什么是定时任务调度? 基于给定的时间点,给定的时间间隔或者给定的执行次数自动执行的任务。 Java中定时任务调动工具有俩: Timer 、Quartz 区别: 出身不同:Timer由jdk直接提供,不需jar包支持;Quartz需引入jar包。 能力不同:Timer简单定时任务;Quartz时间控制功能更强大。 底层机制:Timer只有一个后台线程执行;Quartz是多线程执行任务。 一.Timer简介: 让我们看下官方文档api(JDK1.8) 可能有的小伙伴英语水平不太好,那让我们看下中文版吧: 那么我们可以归纳Timer的定义: 有且仅有一个后台线程对多个业务线程进行定时定频率的调度 主要构件: Timer工具类详解: 二.Timer实战 1.简单实战 1)定时任务类 package com.leo.timer; import java.util.TimerTask; public class MyTimerTask extends TimerTask { private String name; public MyTimerTask(String inputName){ name=inputName; } @Override public void run() { /

SimpleDateFormat showing minutes, seconds and milliseconds wrong

谁说胖子不能爱 提交于 2019-12-29 10:06:27
问题 I have written this sample program where I want to convert a date into another format. I don't see the expected date when using simple date format. public class TestDate { /** * @param args */ public static void main(String[] args) { SimpleDateFormat originalformat = new SimpleDateFormat("yyyy-MM-dd-HH.mm.ss.SSSSSS"); SimpleDateFormat targetformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS"); try { //Use Simple Date Format Date date = originalformat.parse("2015-04-09-17.18.48.677862")

添加事务@Transactional导致整个controller映射路径404

余生颓废 提交于 2019-12-28 14:33:24
记一次代码不规范导致的问题,那时图省事,直接加@Transactional加在控制层,使用dao进行数据保存,没使用service。 问题原因:@Transactional加在控制层 package com . ms . base . controller ; import com . ms . base . dao . WaterMessageDetailMapper ; import com . ms . base . domain . WaterMessageDetail ; import com . ms . base . service . WaterMessageDetailService ; import com . ms . base . utils . ExcelUtil ; import lombok . extern . slf4j . Slf4j ; import org . apache . poi . hssf . usermodel . HSSFDateUtil ; import org . apache . poi . hssf . usermodel . HSSFWorkbook ; import org . apache . poi . ss . usermodel . * ; import org . apache . poi . xssf .

How to parse month full form string using DateFormat in Java?

佐手、 提交于 2019-12-28 11:49:57
问题 I tried this: DateFormat fmt = new SimpleDateFormat("MMMM dd, yyyy"); Date d = fmt.parse("June 27, 2007"); error: Exception in thread "main" java.text.ParseException: Unparseable date: "June 27, 2007" The java docs say I should use four characters to match the full form. I'm only able to use MMM successfully with abbreviated months like "Jun" but i need to match full form. Text: For formatting, if the number of pattern letters is 4 or more, the full form is used; otherwise a short or

How to parse month full form string using DateFormat in Java?

徘徊边缘 提交于 2019-12-28 11:49:31
问题 I tried this: DateFormat fmt = new SimpleDateFormat("MMMM dd, yyyy"); Date d = fmt.parse("June 27, 2007"); error: Exception in thread "main" java.text.ParseException: Unparseable date: "June 27, 2007" The java docs say I should use four characters to match the full form. I'm only able to use MMM successfully with abbreviated months like "Jun" but i need to match full form. Text: For formatting, if the number of pattern letters is 4 or more, the full form is used; otherwise a short or

Synchronizing access to SimpleDateFormat

只谈情不闲聊 提交于 2019-12-28 03:20:40
问题 The javadoc for SimpleDateFormat states that SimpleDateFormat is not synchronized. "Date formats are not synchronized. It is recommended to create separate format instances for each thread. If multiple threads access a format concurrently, it must be synchronized externally." But what is the best approach to using an instance of SimpleDateFormat in a multi threaded environment. Here are a few options I have thought of, I have used options 1 and 2 in the past but I am curious to know if there

What are the date formats available in SimpleDateFormat class?

旧街凉风 提交于 2019-12-27 13:04:12
问题 Can anybody let me know about the date formats available in SimpleDateFormat class. I have gone through api but could not find a satisfactory answer.Any help is highly appreciated. 回答1: Date and time formats are well described below SimpleDateFormat (Java Platform SE 7) - Date and Time Patterns There could be n Number of formats you can possibly make. ex - dd/MM/yyyy or YYYY-'W'ww-u or you can mix and match the letters to achieve your required pattern. Pattern letters are as follow. G - Era

Illegal pattern character 'T' when parsing a date string to java.util.Date

旧巷老猫 提交于 2019-12-27 09:00:07
问题 I have a date string and I want to parse it to normal date use the java Date API,the following is my code: public static void main(String[] args) { String date="2010-10-02T12:23:23Z"; String pattern="yyyy-MM-ddThh:mm:ssZ"; SimpleDateFormat sdf=new SimpleDateFormat(pattern); try { Date d=sdf.parse(date); System.out.println(d.getYear()); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } } However I got an exception: java.lang.IllegalArgumentException: