This article mainly introduces relevant information about the detailed examples of the DateUtils date tool class in Java. Sometimes the date type is used in developing java projects. Here is an introduction to the date tool class. Friends who need it can refer to it
Detailed explanation of examples of DateUtils date tool class in Java
Introduction
在java中队日期类型的处理并不方便,通常都需要借助java.text.SimpleDateFormat类来实现日期类型 和字符串类型之间的转换,但是在jdk1.8之后有所改善,jdk1.7以及之前的版本处理日期类型并不方便, 可以借助Joda Time组件来处理,尤其是日期类型的一些数学操作就更是不方便。
java code
/** * * 日期工具类 java对日期的操作一直都很不理想,直到jdk1.8之后才有了本质的改变。 * 如果使用的是jdk1.8之前版本开发的话,推荐使用Joda Time组件。 * * 特别备注:unix的时间戳单位是秒 * * @author xuyi3 * @2016年7月13日 @下午2:01:00 * @DateUtils * @功能说明: * @春风十里不如你 * @备注 */ public class DateUtils { /** 日志对象 */ private static final Logger logger = LoggerFactory.getLogger(DateUtils.class); /** 年-月-日 时:分:秒 显示格式 */ // 备注:如果使用大写HH标识使用24小时显示格式,如果使用小写hh就表示使用12小时制格式。 public static String DATE_TO_STRING_DETAIAL_PATTERN = "yyyy-MM-dd HH:mm:ss"; /** 年-月-日 显示格式 */ public static String DATE_TO_STRING_SHORT_PATTERN = "yyyy-MM-dd"; private static SimpleDateFormat simpleDateFormat; /** * Date类型转为指定格式的String类型 * * @param source * @param pattern * @return */ public static String DateToString(Date source, String pattern) { simpleDateFormat = new SimpleDateFormat(pattern); return simpleDateFormat.format(source); } /** * * unix时间戳转为指定格式的String类型 * * * System.currentTimeMillis()获得的是是从1970年1月1日开始所经过的毫秒数 * unix时间戳:是从1970年1月1日(UTC/GMT的午夜)开始所经过的秒数,不考虑闰秒 * * @param source * @param pattern * @return */ public static String timeStampToString(long source, String pattern) { simpleDateFormat = new SimpleDateFormat(pattern); Date date = new Date(source * 1000); return simpleDateFormat.format(date); } /** * 将日期转换为时间戳(unix时间戳,单位秒) * * @param date * @return */ public static long dateToTimeStamp(Date date) { Timestamp timestamp = new Timestamp(date.getTime()); return timestamp.getTime() / 1000; } /** * * 字符串转换为对应日期(可能会报错异常) * * @param source * @param pattern * @return */ public static Date stringToDate(String source, String pattern) { simpleDateFormat = new SimpleDateFormat(pattern); Date date = null; try { date = simpleDateFormat.parse(source); } catch (ParseException e) { logger.error("字符串转换日期异常", e); } return date; } /** * 获得当前时间对应的指定格式 * * @param pattern * @return */ public static String currentFormatDate(String pattern) { simpleDateFormat = new SimpleDateFormat(pattern); return simpleDateFormat.format(new Date()); } /** * 获得当前unix时间戳(单位秒) * * @return 当前unix时间戳 */ public static long currentTimeStamp() { return System.currentTimeMillis() / 1000; } //待补充 }
Summary
Master and understand the new features of jdk1.8 and learn to use excellent open source components to achieve your own rapid and efficient development goals.
The above is the detailed content of Tutorial on using DateUtils date tool class in Java programming. For more information, please follow other related articles on the PHP Chinese website!

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version
Chinese version, very easy to use

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function