当前位置:网站首页>Time tool class, get the current time, date to string
Time tool class, get the current time, date to string
2022-07-27 12:37:00 【Clear glaze in the wind】
package com.qiu.tool;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateTools {
private static final String YYYY_MM_DD="yyyy-MM-dd";
private static final String YYYY_MM_DD_HH="yyyy-MM-dd HH";
private static final String HH="HH";
private static final String HH_MM="HH:mm";
private static final String HH_MM_SS="HH:mm:ss";
private static final String HH_MM_SS_A="HH:mm:ss a";
private static final String YYYY_MM_DD_HH_MM="yyyy-MM-dd HH:mm";
private static final String YYYY_MM_DD_HH_MM_SS="yyyy-MM-dd HH:mm:ss";
private static final String YYYY_MM_DD_HH_MM_SS_A="yyyy-MM-dd HH:mm:ss a";
/**
* Get the current time
* @param pattern Format :
* --yyyy-MM-dd;
* --yyyy-MM-dd HH;
* --HH;
* --HH:mm;
* --HH:mm:ss;
* --HH:mm:ss a;
* --yyyy-MM-dd HH:mm;
* --yyyy-MM-dd HH:mm:ss;
* --yyyy-MM-dd HH:mm:ss a
* @return Current time string
*/
public static String getNowTime(String pattern){
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
Date date = new Date();
return sdf.format(date);
}
/**
* Get the current time ( from Calendar Class gets )
* @param pattern Format :
* --yyyy-MM-dd;
* --yyyy-MM-dd HH;
* --HH;
* --HH:mm;
* --HH:mm:ss;
* --HH:mm:ss a;
* --yyyy-MM-dd HH:mm;
* --yyyy-MM-dd HH:mm:ss;
* --yyyy-MM-dd HH:mm:ss a;
* --yyyy year MM month dd HH when mm branch ss second ;
* @return Current time string
*/
public static String getNowTimeFormCalendar(String pattern) {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
return sdf.format(calendar.getTime());
}
/**
* Get the current time ( from Calendar Class gets , Count year, month, day, hour, minute and second )
* @param pattern Format :
* --yyyy-MM-dd;
* --yyyy-MM-dd HH;
* --HH;
* --HH:mm;
* --HH:mm:ss;
* --HH:mm:ss a;
* --yyyy-MM-dd HH:mm;
* --yyyy-MM-dd HH:mm:ss;
*
* @return Current time string
*/
public static String getNowTimeForamCalendarCount(String pattern){
Calendar cal=Calendar.getInstance();
StringBuilder sb=new StringBuilder("");
if(pattern.contains("yyyy")){
sb.append(cal.get(Calendar.YEAR));
sb.append(" year ");
}
if(pattern.contains("MM")){
sb.append(cal.get(Calendar.MONTH));
sb.append(" month ");
}
if(pattern.contains("dd")){
sb.append(cal.get(Calendar.DATE));
sb.append(" Japan ");
}
if(pattern.contains("HH")){
sb.append(cal.get(Calendar.HOUR_OF_DAY));
sb.append(" when ");
}
if(pattern.contains("mm")){
sb.append(cal.get(Calendar.MINUTE));
sb.append(" branch ");
}
if(pattern.contains("ss")){
sb.append(cal.get(Calendar.SECOND));
sb.append(" second ");
}
return sb.toString();
}
/**
* Get a few days
* @param number Days
* @return A few days later
*/
public static Date getDateDaysAfter(int number){
return getDateAround(+number,Calendar.DATE);
}
/**
* Get a few days ago
* @param number Days
* @return A few days ago
*/
public static Date getDateDaysBefore(int number){
return getDateAround(-number,Calendar.DATE);
}
/**
* Get a few months later
* @param number Monthly number
* @return A few months later
*/
public static Date getDateMonthAfter(int number){
return getDateAround(+number,Calendar.MONTH);
}
/**
* Get the time a few months ago
* @param number Monthly number
* @return A few months ago
*/
public static Date getDateMonthBefore(int number){
return getDateAround(-number,Calendar.MONTH);
}
/**
* Get a few years later
* @param number number of years
* @return A few years later
*/
public static Date getDateYearAfter(int number){
return getDateAround(+number,Calendar.YEAR);
}
/**
* Get the time a few years ago
* @param number number of years
* @return A few years ago
*/
public static Date getDateYearBefore(int number){
return getDateAround(-number,Calendar.YEAR);
}
/**
*
* @param number Positive number is plus , Negative is minus
* @param dateUnit Time single temperature :
* --Calendar.YEAR year ;
* --Calendar.MONTH month ;
* --Calendar.DATE Japan ;
* --Calendar.HOUR when ;
* --Calendar.MINUTE branch ;
*
* @return
*/
private static Date getDateAround(int number,int dateUnit){
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.add(dateUnit, number);
return calendar.getTime();
}
/**
* date Time to format
* @param date date data
* @param pattern Format
* @return Formatted time string
*/
public static String dateToFormatString(Date date,String pattern){
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
String format = sdf.format(date.getTime());
return format;
}
}
边栏推荐
- Chain representation and implementation of queues
- 概述静态内部类与非静态内部类
- js日期时间格式化(年月日、时分秒、星期、季度、获取时间差、日期与时间戳转换的功能)
- 评价自动化测试优劣的隐性指标
- What should I do if I can't see any tiles on SAP Fiori launchpad?
- About offline caching application cache / using manifest file caching
- The strongest distributed locking tool: redisson
- Recursive method | Fibonacci sequence
- Chapter 7 exception handling
- Why does MySQL index use b+ tree instead of jump table?
猜你喜欢

Basic architecture of data Lake
![[database data recovery] a data recovery case in which the disk partition where the SQL Server database is located is insufficient and an error is reported](/img/8a/478209854cb5ce139afc338d106d36.png)
[database data recovery] a data recovery case in which the disk partition where the SQL Server database is located is insufficient and an error is reported

Complete data summary of lapsus$apt organization that stole Microsoft's source code in March 2022

How to use the server to build our blog

Chapter 10 enumeration classes and annotations

Security measures for tcp/ip protocol vulnerabilities

Interviewer: how to deal with the data loss of redis master-slave cluster switching?

Advance in the flutter project_ image_ Picker component usage

Good looking (dynamic) Jay fan self-made dynamic album card (front and back are different) and lyrics page

Solution: the idea project does not display a tree view
随机推荐
Detailed explanation of flask framework
String to realize fuzzy query
概述静态内部类与非静态内部类
How to design a secure external interface
Photoshop web design tutorial
Chapter 10 enumeration classes and annotations
详述throw与throws
正则表达式去除两端空格
HDU1698_Just a Hook
Mysql8msi installation tutorial (database mysql installation tutorial)
20210518-Cuda
Mixin\ plug in \scoped style
Uniapp video video playback is not completed. It is forbidden to drag the progress bar fast forward
详述HashSet的add方法
20210520 TCP sliding window
The sparksubmit. Main () method submits external parameters and remotely submits the standalone cluster task
Set接口
How to ask questions on the road for the first time - necessary skills for self-study (with live playback)
Solve the problem of @onetomany query falling into circular reference
Does the service layer need an interface