当前位置:网站首页>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;
}
}
边栏推荐
- MySQL paging query instance_ MySQL paging query example explanation "suggestions collection"
- Does the service layer need an interface
- Multi activity disaster recovery construction after station B 713 accident | takintalks share
- 20210520 TCP sliding window
- Error: the source of the existing CacheManager is: urlconfigurationsource
- Detailed explanation of deeplab series (simple and practical annual summary)
- Enjoy the luxury meta universe louis:the game, and participate in the NFT series digital collection white roll activity | tutorial
- Mixin\ plug in \scoped style
- 详述jdbc查询方法执行过程
- 20210408 longest public prefix
猜你喜欢

Unity 2D game tutorial

The strongest distributed locking tool: redisson

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

Solution: can not issue executeupdate() or executelargeupdate() for selections

Play CSDN editor

详述try-catch-finally

Xposed+FDex2 app脱壳 (黑猫投诉app脱壳)

C program debugging and exception handling (try catch)

What should I do if I can't see any tiles on SAP Fiori launchpad?

The sparksubmit. Main () method submits external parameters and remotely submits the standalone cluster task
随机推荐
V. introduction of other objectives and general options
Basic architecture of data Lake
Implicit indicators for evaluating the advantages and disadvantages of automated testing
SSM实战项目-前后分离(简单易懂)
@Postconstruct annotations and initializingbean perform some initialization operations after bean instantiation
详述jdbc查询方法执行过程
P1321 word overlay restore [getting started]
Map接口
2021-4-7-find the maximum water holding capacity
Common usage of curl command
CLS monitoring alarm: ensure high availability of online services in real time
JVM memory layout detailed, illustrated, well written!
Self built personalized automatic quotation system to cope with changeable quotation mode
20210520 TCP sliding window
Soft core microprocessor
JVM memory model
Multi activity disaster recovery construction after station B 713 accident | takintalks share
Plus版SBOM:流水线物料清单PBOM
Insert sort summary
Several rounds of SQL queries in a database