java时间格式化

1,CST时间格式化,这个一般是返回值的类型为 Date 类型,如果不做处理,返给前端的就是时间戳,当然也可以更改返回值类型为 String,这样就不用处理了。方法如下:

复制代码
    /**
    * 格式化时间
    * @param date  Thu Sep 28 11:51:40 CST 2023
    * @return      2023-09-28 11:51:40
    */
   public static String CSTTime(String date)  {
//    只有年月日
//    DateFormat formate = new SimpleDateFormat("yyyy-MM-dd");
//    精确到时分秒
      DateFormat formate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      DateFormat dateFormate = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH);
      Date dateTime = null;
      try {
         dateTime = dateFormate.parse(date);
      } catch (ParseException e) {
         logger.error("日期格式化失败",e.getMessage(),e);
      }
      String dateString = formate.format(dateTime);
      return dateString;
   }

2,日期增加汉字,方法如下:

**** 常量都写在这里,下面的方法用到的常量都在此处 ****

复制代码
public static final SimpleDateFormat TIME_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public static final SimpleDateFormat TIME_FORMAT_Type = new SimpleDateFormat("yyyyMMddHHmmss");
public static final SimpleDateFormat TIME_FORMAT_MINUTE_Type = new SimpleDateFormat("yyyyMMddHHmm");
public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
public static final DateFormat DATE_FORMAT_Z = new SimpleDateFormat("yyyy年MM月dd日");
public static final DateFormat DATE_FORMAT_d = new SimpleDateFormat("yyyy.MM.dd");
复制代码
/**
 * 格式化时间
 * @param date 2020-02-02
 * @return     2020年02月02日
 */
public static String strDate(String date){
   Date parse = null;
   try {
           if (StringUtil.isNotBlank(date)) {
               parse = DATE_FORMAT.parse(date);
           } else {
               return "";
           }
   } catch (ParseException e) {
      logger.error("日期格式化失败",e.getMessage(),e);
   }

   return DATE_FORMAT_Z.format(parse);
}

3,时间格式由 "-" 变成 "." ,代码如下:

复制代码
/**
 * 格式化时间
 * @param date 2020-02-02
 * @return     2020.02.02
 */
public static String strDated(String date){
   Date parse = null;
   try {
           if (StringUtil.isNotBlank(date)) {
               parse = DATE_FORMAT.parse(date);
           } else {
               return "";
           }

   } catch (ParseException e) {
      logger.error("日期格式化失败",e.getMessage(),e);
   }

   return DATE_FORMAT_d.format(parse);
}

4,斜杆时间并去掉前面的0

复制代码
/**
 * 格式化时间
 * @param date 2020-02-02
 * @return     2020/2/2
 */
public static String strDate6(String date){
   SimpleDateFormat formatter = new SimpleDateFormat("yyyy/M/d");
   Date currentTime = null;
   try {
      currentTime = DATE_FORMAT.parse(date);
   } catch (ParseException e) {
      e.printStackTrace();
   }
   //得到当前系统时间
   return formatter.format(currentTime); //将日期时间格式化
}

5,前面加 0

复制代码
/**
 * 格式化时间
 * @param date 2020-2-2
 * @return     2020-02-02
 */
public static String strDate1(String date){
   Date parse = null;
   try {
           if (StringUtil.isNotBlank(date)) {
               parse = DATE_FORMAT.parse(date);
           } else {
               return "";
           }
   } catch (ParseException e) {
      logger.error("日期格式化失败",e.getMessage(),e);
   }
   return DATE_FORMAT.format(parse);
}

6,没有符合的时间格式转时间,这个不常用

复制代码
/**
 * 格式化时间
 * @param date 20230927190901
 * @return     2023-09-27 19:09:01
 */
public static String strDate2(String date){
   Date parse = null;
   try {
           if (StringUtil.isNotBlank(date)) {
               parse = TIME_FORMAT_Type.parse(date);
           } else {
               return "";
           }
   } catch (ParseException e) {
      logger.error("日期格式化失败",e.getMessage(),e);
   }
   return TIME_FORMAT.format(parse);
}

7,去掉时分秒

复制代码
/**
 * 格式化时间
 * @param date 2020-02-02 19:09:01
 * @return     2020-02-02
 */
public static String strDate4(String date){
   Date parse = null;
   try {
           if (StringUtil.isNotBlank(date)) {
               parse = TIME_FORMAT.parse(date);
           } else {
               return "";
           }

   } catch (ParseException e) {
      logger.error("日期格式化失败",e.getMessage(),e);
   }
   return DATE_FORMAT.format(parse);
}

8,时间和时间戳相互转换,这个都写在一起了,需要的自己改一下就行

复制代码
/**
 *
 * @param timeType    时间格式 yyyy-MM-dd HH:mm:ss
 * @param timestamp   时间戳 1695794616000
 * @param time        时间 2023-09-27 22:22:22
 * @return
 */
public static void timeAndTimestamp(String timeType,String timestamp,String time){
   SimpleDateFormat simpleDateFormat = new SimpleDateFormat(timeType);
   String format = simpleDateFormat.format(new Date(Long.parseLong(timestamp)));
   System.out.println("时间戳转时间:"+ format );  
   try {
      SimpleDateFormat sdf = new SimpleDateFormat(timeType);
      String s = String.valueOf(sdf.parse(time).getTime()/1000);
      System.out.println("时间转时间戳:"+ s);
   } catch (Exception e) {
      e.printStackTrace();
   }
}

结果:

时间戳转时间:2023-09-27 14:03:36

时间转时间戳:1695824542

相关推荐
述雾学java几秒前
Spring Cloud Feign 整合 Sentinel 实现服务降级与熔断保护
java·spring cloud·sentinel
保持学习ing几秒前
苍穹外卖day3--公共字段填充+新增菜品
java·阿里云·实战·springboot·前后端·外卖项目·阿里云文件存储
77qqqiqi19 分钟前
正则表达式
java·后端·正则表达式
厦门德仔44 分钟前
【WPF】WPF(样式)
android·java·wpf
大春儿的试验田44 分钟前
高并发收藏功能设计:Redis异步同步与定时补偿机制详解
java·数据库·redis·学习·缓存
Gappsong8741 小时前
【Linux学习】Linux安装并配置Redis
java·linux·运维·网络安全
hqxstudying1 小时前
Redis为什么是单线程
java·redis
RainbowSea1 小时前
NVM 切换 Node 版本工具的超详细安装说明
java·前端
逆风局?1 小时前
Maven高级——分模块设计与开发
java·maven
周某某~1 小时前
maven详解
java·maven