【Date和String时间格式转换】

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 时间日期相关工具类
 **/
public class DateUtil {

  private Logger LOGGER = LoggerFactory.getLogger(this.getClass());
  public static final String SHORT_DATETIME = "yyyyMMddHHmmss";
  public static final String SHORT_DATETIME2 = "yyyyMMddHHmm";
  public static String DATA_TIME_TUC_0_TIME = "yyyy-MM-dd'T'HH:mm:ss.SSS+00:00";
  public static String DAY_HOUR_MIN_CHN = "dd'日'HH':'mm'分'";
  private static final String GMT = "EEE,d MMM yyyy HH:mm:ss 'GMT'";
  public static final String MILLISECOND = "yyyy-MM-dd HH:mm:ss.SSS";
  public static final String ISO_DATETIME = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";
  public static String DATA_TIME_TUC_8_TIME = "yyyy-MM-dd'T'HH:mm:ss.SSS+08:00";
  public static String DATA_TIME_TUC_8_MIN = "yyyy-MM-dd'T'00:00:00.000+08:00";
  public static String DATA_TIME_TUC_8_MAX = "yyyy-MM-dd'T'23:59:59.999+08:00";
  public static final String DATE_MONTH = "yyyy-MM";
  public static final String DATE_DAY = "yyyy-MM-dd";
  public static final String DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
  public static final String DAY_OF_MAX = "yyyy-MM-dd 23:59:59";
  public static final String DAY_OF_MIN = "yyyy-MM-dd 00:00:00";
  public static SimpleDateFormat MILLISECOND_FORMAT = new SimpleDateFormat(MILLISECOND);
  public static SimpleDateFormat ISO_DATETIME_FORMAT = new SimpleDateFormat(ISO_DATETIME);
  public static SimpleDateFormat default_sdf4 = new SimpleDateFormat(DATA_TIME_TUC_8_TIME);
  public static SimpleDateFormat DATA_TIME_TUC_8_MIN_FORMAT = new SimpleDateFormat(DATA_TIME_TUC_8_MIN);
  public static SimpleDateFormat DATA_TIME_TUC_8_MAX_FORMAT = new SimpleDateFormat(DATA_TIME_TUC_8_MAX);
  public static SimpleDateFormat DATE_MONTH_FORMAT = new SimpleDateFormat(DATE_MONTH);
  public static SimpleDateFormat DATE_DAY_FORMAT = new SimpleDateFormat(DATE_DAY);
  public static SimpleDateFormat DATETIME_FORMAT_FORMAT = new SimpleDateFormat(DATETIME_FORMAT);
  public static SimpleDateFormat DAY_OF_MAX_FORMAT = new SimpleDateFormat(DAY_OF_MAX);
  public static SimpleDateFormat DAY_OF_MIN_FORMAT = new SimpleDateFormat(DAY_OF_MIN);

  /**
   * @param dateString 时间字符串
   * @param format     字符串的格式
   * @return java.util.Date
   * @author GHX
   * @date 2022/11/3 10:28
   */
  public static Date changeStringToDate(String dateString, SimpleDateFormat format) {
    if (StringUtils.isBlank(dateString)) {
      return null;
    }
    try {
      return format.parse(dateString);
    } catch (Exception e) {
      return null;
    }
  }
  /**
   * @param date   date类型时间
   * @param format 需要格式成的目标格式
   * @return java.lang.String
   * @author GHX
   * @date 2022/11/3 10:29
   */
  public static String changeDateToString(Date date, SimpleDateFormat format) {
    if (date == null) {
      return null;
    }
    return format.format(date);
  }
}

测试及结果打印

   //当天起止时间
  public static void getTodayTime() {
    String date= "2022-11-03T23:59:59.999+08:00";
    String date2= "2022-11-03 00:00:00";
    Date endTime = changeStringToDate(date, DATA_TIME_TUC_8_TIME_FORMAT);
    Date startTime = changeStringToDate(date2, DATETIME_FORMAT_FORMAT);
    System.out.println("today  startTime : " + startTime + " endTime : " + endTime);
    return;
  }

  //七天起止时间
  public static void getWeekTime() {
    Calendar instance = Calendar.getInstance();
    String endTime = changeDateToString(instance.getTime(), DATA_TIME_TUC_8_MAX_FORMAT);
    instance.add(Calendar.DAY_OF_YEAR, -7);
    String startTime = changeDateToString(instance.getTime(), DATA_TIME_TUC_8_MIN_FORMAT);
    System.out.println("week  startTime : " + startTime + " endTime : " + endTime);
    return;
  }

  public static void main(String[] args) {
    getTodayTime();
    getWeekTime();
  }

today startTime : Thu Nov 03 00:00:00 CST 2022 endTime : Thu Nov 03 23:59:59 CST 2022

week startTime : 2022-10-27T00:00:00.000+08:00 endTime : 2022-11-03T23:59:59.999+08:00

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qdu_ghx

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值