Java中的日期Date
来源:互联网 发布:linux查询端口命令 编辑:程序博客网 时间:2024/06/06 10:38
在项目开发中,日期处理是一种很常见的操作,在这总结了一下,很简单,留着以后备用。
package com.lsh.utils;import java.text.DateFormat;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;public class DateUtil { //SimpleDateFormate 是线程不安全的 public static ThreadLocal<DateFormat> yyyyMMdd = new ThreadLocal<DateFormat>(){ protected synchronized DateFormat initialValue() { return new SimpleDateFormat("yyyyMMdd"); } }; public static ThreadLocal<DateFormat> yyyyMMddLine = new ThreadLocal<DateFormat>(){ protected synchronized DateFormat initialValue() { return new SimpleDateFormat("yyyy/MM/dd"); } }; public static ThreadLocal<DateFormat> YYMMDD = new ThreadLocal<DateFormat>(){ protected synchronized DateFormat initialValue() { return new SimpleDateFormat("yyyyMMdd"); } }; public static ThreadLocal<DateFormat> HHmmss =new ThreadLocal<DateFormat>(){ protected synchronized DateFormat initialValue(){ return new SimpleDateFormat("HHmmss"); } }; public static ThreadLocal<DateFormat> yyyyMMdd10 = new ThreadLocal<DateFormat>(){ protected synchronized DateFormat initialValue() { return new SimpleDateFormat("yyyy-MM-dd"); } }; public static ThreadLocal<DateFormat> yyyyMMddHHmmss = new ThreadLocal<DateFormat>(){ protected synchronized DateFormat initialValue() { return new SimpleDateFormat("yyyyMMddHHmmss"); } }; public static ThreadLocal<DateFormat> formateter17 = new ThreadLocal<DateFormat>(){ protected synchronized DateFormat initialValue() { return new SimpleDateFormat("yyyyMMddHHmmssSSS"); } }; public static ThreadLocal<DateFormat> formateter18 = new ThreadLocal<DateFormat>(){ protected synchronized DateFormat initialValue() { return new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); } }; public static ThreadLocal<DateFormat> formateter19 = new ThreadLocal<DateFormat>(){ protected synchronized DateFormat initialValue() { return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); } }; public static ThreadLocal<DateFormat> hhmmssSSS = new ThreadLocal<DateFormat>(){ protected synchronized DateFormat initialValue() { return new SimpleDateFormat("hhmmssSSS"); } }; public static SimpleDateFormat getDateFormat6(){ DateFormat df = HHmmss.get(); if(df == null){ df = new SimpleDateFormat("HHmmss"); HHmmss.set(df); } return (SimpleDateFormat)df; } public static SimpleDateFormat getDateFormat8(){ DateFormat df = yyyyMMdd.get(); if(df == null){ df = new SimpleDateFormat("yyyyMMdd"); yyyyMMdd.set(df); } return (SimpleDateFormat)df; } public static SimpleDateFormat getDateFormat10(){ DateFormat df = yyyyMMdd10.get(); if(df == null){ df = new SimpleDateFormat("yyyy-MM-dd"); yyyyMMdd10.set(df); } return (SimpleDateFormat)df; } public static SimpleDateFormat getDateFormat14(){ DateFormat df = yyyyMMddHHmmss.get(); if(df == null){ df = new SimpleDateFormat("yyyyMMddHHmmss"); yyyyMMddHHmmss.set(df); } return (SimpleDateFormat)df; } public static SimpleDateFormat getDateFormat17(){ DateFormat df = formateter17.get(); if(df == null){ df = new SimpleDateFormat("yyyyMMddHHmmssSSS"); formateter17.set(df); } return (SimpleDateFormat)df; } public static SimpleDateFormat getDateFormat18(){ DateFormat df = formateter18.get(); if(df == null){ df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); formateter18.set(df); } return (SimpleDateFormat)df; } public static SimpleDateFormat getDateFormat19(){ DateFormat df = formateter19.get(); if(df == null){ df = new SimpleDateFormat("yyyy-MM-dd HH:mm:SSS"); formateter19.set(df); } return (SimpleDateFormat)df; } /** * 获取制定对象的format对象 * @param args */ public static SimpleDateFormat getsimSimpleDateFormatByPattern(final String pattern){ ThreadLocal<DateFormat> formatter = new ThreadLocal<DateFormat>(){ @Override protected synchronized DateFormat initialValue() { return new SimpleDateFormat(); } }; return (SimpleDateFormat)formatter.get(); } /** * 获取day天后的日期 * @param args */ public static Date getDateAfter(Date date,int day){ Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set(Calendar.DATE, calendar.get(Calendar.DATE)+day); return calendar.getTime(); } public static void main(String[] args) { System.out.println(DateUtil.getDateFormat6().format(new Date())); System.out.println(DateUtil.getDateFormat8().format(new Date())); System.out.println(DateUtil.getDateFormat10().format(new Date())); System.out.println(DateUtil.getDateFormat14().format(new Date())); System.out.println(DateUtil.getDateFormat17().format(new Date())); System.out.println(DateUtil.getDateFormat18().format(new Date())); System.out.println(DateUtil.getDateFormat19().format(new Date())); System.out.println(DateUtil.getDateFormat18().format(getDateAfter(new Date(), 100))); }}
阅读全文
0 0
- Java中的日期Date
- Java中的日期Date处理
- java中的日期类Date
- java中的日期类Date
- java date 日期 date
- Java 中的日期时间 date、calendar
- JavaScript中的日期(Date)
- Java Date 日期排序
- Java date日期操作
- java date日期操作
- Java Date日期比较
- java date 日期操作
- java Date 日期格式
- java Date 日期 格式化
- Java Date日期相关
- JAVA中的日期处理(Calendar类,Date类)
- Java中的日期/时间类,Date、DateFormat、Calendar类
- Java中的日期/时间类,Date、DateFormat、Calendar类
- 2.java编程实现压缩文法的等价变化--加标记算法
- 九九乘法表
- SourceTree 免登陆操作
- 第二部分Linux 文件、目录与磁盘格式笔记(未完全,待续 缺少磁盘分区部分的练习)
- 妈耶,搞了半天原来pycharm整体字体太小了应该这样设置。。
- Java中的日期Date
- 从零开始前端学习[26]:html5的一些常用标签,header,footer,section,aside,figure,figcaption,nav,artical,
- 爬虫笔记(9/27)------常用爬虫(图片,链接,内容)
- Python_解析xml
- 1248 寒冰王座 换个思维来解决这个背包题目
- MOOC清华《VC++面向对象与可视化程序设计》第3章:Windows文本-例(2)字体、颜色、下划线
- HDOJ2031(十进制变X进制)
- Hql
- [python]leetcode(22). Generate Parentheses