Skip to content

Commit

Permalink
添加时间格式化
Browse files Browse the repository at this point in the history
  • Loading branch information
ichtj committed Mar 28, 2024
1 parent 808132d commit d594d1a
Showing 1 changed file with 22 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.face_chtj.base_iotutils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
Expand All @@ -12,8 +11,7 @@
* author chtj
* desc 时间工具类
*
* {@link #tsToMs(int)} yyyy-MM-dd HH:mm:ss
* {@link #tsToYMD(int)} yyyy-MM-dd
* {@link #timestampToDate(String,long)} 获取今天年月日
* {@link #getTodayDate()} 获取今天年月日
* {@link #getTodayDateHms(String)} 获取今天年月日 可添加格式
* {@link #getTime()} 获取当前系统的时间戳
Expand All @@ -24,36 +22,36 @@
*
*/
public class TimeUtils {
public static final String TIME_YMD="yyyy-MM-dd";
public static final String TIME_YMDHM="yyyy-MM-dd HH:mm";
public static final String TIME_YMDHMS="yyyy-MM-dd HH:mm:ss";
public static final String TIME_YMDHMS_CN="yyyy年MM月dd日 HH时mm分ss秒";
public static final String TIME_YMDHMS_UNSIGNED="yyyyMMddHHmmss";
/**
* 时间戳 转 String类型的精确到时分秒
* @param time
* @return
*/
public static String tsToMs(int time) {
long time1000 = Long.parseLong(String.valueOf(time)) * 1000;
return new SimpleDateFormat(TIME_YMDHMS, Locale.CHINA).format(time1000);
}

// public static final String TIME_YMD="yyyy-MM-dd";
// public static final String TIME_YMDHM="yyyy-MM-dd HH:mm";
// public static final String TIME_YMDHMS="yyyy-MM-dd HH:mm:ss";
// public static final String TIME_YMDHMS_CN="yyyy年MM月dd日 HH时mm分ss秒";
// public static final String TIME_YMDHMS_UNSIGNED="yyyyMMddHHmmss";
public static final String DATE_FORMAT_MERGE = "yyyyMMddHHmmss"; // 默认格式,包括日期和时间
public static final String DATE_FORMAT_DEFAULT = "yyyy-MM-dd HH:mm:ss"; // 默认格式,包括日期和时间
public static final String DATE_FORMAT_DATE_ONLY = "yyyy-MM-dd"; // 仅包含日期,不包括时间
public static final String DATE_FORMAT_TIME_ONLY = "HH:mm:ss"; // 仅包含时间,不包括日期
public static final String DATE_FORMAT_CUSTOM = "yyyy/MM/dd HH-mm-ss"; // 自定义格式
public static final String DATE_FORMAT_ISO_8601 = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"; // ISO 8601格式
public static final String DATE_FORMAT_SHORT_DATE = "MM/dd/yyyy"; // 短日期格式,例如:03/28/2024
public static final String DATE_FORMAT_SHORT_DATE_TIME = "MM/dd/yyyy HH:mm"; // 短日期时间格式,例如:03/28/2024 15:30
public static final String DATE_FORMAT_SHORT_TIME = "HH:mm"; // 短时间格式,例如:15:30
public static final String DATE_FORMAT_WEEKDAY = "EEEE"; // 星期几,例如:星期一、星期二
public static final String DATE_FORMAT_MONTH = "MMMM"; // 月份,例如:一月、二月
public static final String DATE_FORMAT_YEAR = "yyyy"; // 年份,例如:2024
/**
* 时间戳 转 String类型的年月日
* @param time
* @return
*/
public static String tsToYMD(int time) {
long time1000 = Long.parseLong(String.valueOf(time)) * 1000;
return new SimpleDateFormat(TIME_YMD, Locale.CHINA).format(time1000);
public static String timestampToDate(String pattern,long time) {
return new SimpleDateFormat(pattern, Locale.CHINA).format(time * 1000);
}
/**
* 获取今天年月日
* @return 2017-08-14
*/
public static String getTodayDate() {
return new SimpleDateFormat(TIME_YMD, Locale.CHINA).format(new Date());
return new SimpleDateFormat(DATE_FORMAT_DATE_ONLY, Locale.CHINA).format(new Date());
}

/**
Expand Down Expand Up @@ -87,7 +85,7 @@ public static long getTimeMs() {
*/
public static String getTodayAddMonthDate(int month) {
try {
SimpleDateFormat sdf = new SimpleDateFormat(TIME_YMD);
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_DATE_ONLY);
Date now =sdf.parse(getTodayDate());
Calendar calendar = Calendar.getInstance();
calendar.setTime(now);
Expand Down

0 comments on commit d594d1a

Please sign in to comment.