前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >将string字符串转成LocalDateTime工具类

将string字符串转成LocalDateTime工具类

作者头像
高大北
发布2022-06-14 20:30:48
发布2022-06-14 20:30:48
4.1K00
代码可运行
举报
文章被收录于专栏:java架构计划训练营
运行总次数:0
代码可运行
代码语言:javascript
代码运行次数:0
复制
```
public class LocalDateTimeUtil {
/**
 * 将Long类型的时间戳转换成String 类型的时间格式,时间格式为:yyyy-MM-dd HH:mm:ss
 */
public static String convertTimeToString(Long time){
    if (StringUtils.isEmpty(time)){
        return null;
    }
    DateTimeFormatter ftf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    return ftf.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault()));
}

/**
 * 将Long类型的时间戳转换成String 类型的时间格式,时间格式为:yyyy-MM-dd
 */
public static String convertTimeToStringYMD(Long time){
    if (StringUtils.isEmpty(time)){
        return null;
    }
    DateTimeFormatter ftf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    return ftf.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault()));
}


/**
 * 将字符串转日期成Long类型的时间戳,格式为:yyyy-MM-dd HH:mm:ss
 */
public static Long convertTimeToLong(String time) {
    if (StringUtils.isEmpty(time)){
        throw new IllegalArgumentException("时间参数异常!");
    }
    DateTimeFormatter ftf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    LocalDateTime parse = LocalDateTime.parse(time, ftf);
    return LocalDateTime.from(parse).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
}

/**
 * 将字符串转日期成Long类型的时间戳,格式为:yyyy-MM-dd
 */
public static Long convertTimeToLongYMD(String time) {
    if (StringUtils.isEmpty(time)){
        throw new IllegalArgumentException("时间参数异常!");
    }
    DateTimeFormatter ftf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    LocalDateTime parse = LocalDateTime.parse(time, ftf);
    return LocalDateTime.from(parse).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
}

/**
 * 将日期转换为字符串,格式为:yyyy-MM-dd HH:mm:ss
 * @param localDateTime
 * @return
 */
public static  String convertDateToString(LocalDateTime localDateTime){
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    String dateTime = dtf.format(localDateTime);
    return dateTime;
}

/**
 * 将日期转换为字符串,格式为:yyyy-MM-dd
 * @param localDateTime
 * @return
 */
public static  String convertDateToStringYMD(LocalDateTime localDateTime){
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    String dateTime = dtf.format(localDateTime);
    return dateTime;
}

/**
 * 将字符串转换为日期,格式为:yyyy-MM-dd HH:mm:ss
 * @param time
 * @return
 */
public static LocalDateTime convertStringToDate(String time){
    DateTimeFormatter dft = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    LocalDateTime dateTime = LocalDateTime.parse(time, dft);
    return dateTime;
}

/**
 * 将字符串转换为日期,格式为:yyyy-MM-dd
 * @param time
 * @return
 */
public static LocalDateTime convertStringToDateYMD(String time){
    DateTimeFormatter dft = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    LocalDateTime dateTime = LocalDateTime.parse(time, dft);
    return dateTime;
}

/**
 * 取本月第一天
 */
public static LocalDate firstDayOfThisMonth() {
    LocalDate today = LocalDate.now();
    return today.with(TemporalAdjusters.firstDayOfMonth());
}

/**
 * 取本月第N天
 */
public static LocalDate dayOfThisMonth(int n) {
    LocalDate today = LocalDate.now();
    return today.withDayOfMonth(n);
}

/**
 * 取本月最后一天
 */
public static LocalDate lastDayOfThisMonth() {
    LocalDate today = LocalDate.now();
    return today.with(TemporalAdjusters.lastDayOfMonth());
}

/**
 * 取本月第一天的开始时间
 */
public static LocalDateTime startOfThisMonth() {
    return LocalDateTime.of(firstDayOfThisMonth(), LocalTime.MIN);
}


/**
 * 取本月最后一天的结束时间
 */
public static LocalDateTime endOfThisMonth() {
    return LocalDateTime.of(lastDayOfThisMonth(), LocalTime.MAX);
}
```
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档