当前位置:网站首页>时间工具类,得到当前时间,date转string
时间工具类,得到当前时间,date转string
2022-07-27 12:32:00 【风间净琉璃】
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";
/**
* 获取当前时间
* @param pattern 格式:
* --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 当前时间字符串
*/
public static String getNowTime(String pattern){
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
Date date = new Date();
return sdf.format(date);
}
/**
* 获取当前时间(从Calendar类获取)
* @param pattern 格式:
* --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年MM月dd HH时mm分ss秒;
* @return 当前时间字符串
*/
public static String getNowTimeFormCalendar(String pattern) {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
return sdf.format(calendar.getTime());
}
/**
* 获取当前时间(从Calendar类获取,统计年月日时分秒)
* @param pattern 格式:
* --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 当前时间字符串
*/
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("年");
}
if(pattern.contains("MM")){
sb.append(cal.get(Calendar.MONTH));
sb.append("月");
}
if(pattern.contains("dd")){
sb.append(cal.get(Calendar.DATE));
sb.append("日");
}
if(pattern.contains("HH")){
sb.append(cal.get(Calendar.HOUR_OF_DAY));
sb.append("时");
}
if(pattern.contains("mm")){
sb.append(cal.get(Calendar.MINUTE));
sb.append("分");
}
if(pattern.contains("ss")){
sb.append(cal.get(Calendar.SECOND));
sb.append("秒");
}
return sb.toString();
}
/**
* 得到几天后的时间
* @param number 天数
* @return 几天后的时间
*/
public static Date getDateDaysAfter(int number){
return getDateAround(+number,Calendar.DATE);
}
/**
* 得到几天前的时间
* @param number 天数
* @return 几天前的时间
*/
public static Date getDateDaysBefore(int number){
return getDateAround(-number,Calendar.DATE);
}
/**
* 得到几个月后的时间
* @param number 月数
* @return 几月后的时间
*/
public static Date getDateMonthAfter(int number){
return getDateAround(+number,Calendar.MONTH);
}
/**
* 得到几月前的时间
* @param number 月数
* @return 几月前的时间
*/
public static Date getDateMonthBefore(int number){
return getDateAround(-number,Calendar.MONTH);
}
/**
* 得到几年后的时间
* @param number 年数
* @return 几年后的时间
*/
public static Date getDateYearAfter(int number){
return getDateAround(+number,Calendar.YEAR);
}
/**
* 得到几年前的时间
* @param number 年数
* @return 几年前的时间
*/
public static Date getDateYearBefore(int number){
return getDateAround(-number,Calendar.YEAR);
}
/**
*
* @param number 正数为加,负数为减
* @param dateUnit 时间单温:
* --Calendar.YEAR 年;
* --Calendar.MONTH 月;
* --Calendar.DATE 日;
* --Calendar.HOUR 时;
* --Calendar.MINUTE 分;
*
* @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转格式化的时间
* @param date date数据
* @param pattern 格式
* @return 格式化的时间字符串
*/
public static String dateToFormatString(Date date,String pattern){
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
String format = sdf.format(date.getTime());
return format;
}
}
边栏推荐
- 2021-3-17-byte-hu Pai
- Fundamentals of mathematics 02 - sequence limit
- When the script runs in the background, it redirects the log from the console to its own named file
- Play CSDN editor
- JVM memory layout detailed, illustrated, well written!
- Configuration files in MySQL
- 银行人脸识别系统被攻破:一储户被偷走 43 万元
- Top 10 international NFT exchanges
- Complete data summary of lapsus$apt organization that stole Microsoft's source code in March 2022
- P1321 word overlay restore [getting started]
猜你喜欢
Do you really understand the underlying data structure skip list of Zset in redis?

Go Beginner (4)

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

Shake quickly to rescue the "frustrated person"

详述try-catch-finally

Wechat applet session holding

Solution: the idea project does not display a tree view

V. introduction of other objectives and general options

Shutter project scrollcontroller attached to multiple scroll views, failed assertion: line 109 POS 12 error handling

Top 10 international NFT exchanges
随机推荐
Detailed explanation of deeplab series (simple and practical annual summary)
CLS monitoring alarm: ensure high availability of online services in real time
Do you really understand the underlying data structure skip list of Zset in redis?
CMD Chinese garbled code solution
Kazoo tutorial
系统临时文件的写和读:createTempFile和tempFileContent[通俗易懂]
Detailed explanation of flask framework
浪潮之巅——读书笔记+摘录+感悟
The song of the virtual idol was originally generated in this way!
Chapter 7 exception handling
The configuration change removed the routing filter, and the distributed router was overwhelmed: the Canadian network was paralyzed
Implicit indicators for evaluating the advantages and disadvantages of automated testing
Shutter project scrollcontroller attached to multiple scroll views, failed assertion: line 109 POS 12 error handling
Transactions in MySQL
硬刚甲方后:中国移动 4908 万大单被废
Data Lake (20): Flink is compatible with iceberg, which is currently insufficient, and iceberg is compared with Hudi
Chapter 8 multithreading
20210422 consolidation interval
ArrayList常用方法总结
5V boost 9V chip