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

相关推荐
考虑考虑3 小时前
JDK27结构化并发
java·后端·java ee
Cosolar6 小时前
监控指标全正常,订单接口从 80ms 涨到 4s 故障复盘
java·后端·面试
会编程的吕洞宾15 小时前
Spring AI 2.0 工具调用实战:让 AI 自动调用你的 Java 方法
java·人工智能·经验分享·后端·spring
斯维赤15 小时前
SELECT * 为什么是覆盖索引的天敌?
java·后端·mysql
stormsha15 小时前
库存 5,10 个并发各买 1 件:为什么最后不剩 -5 件
java·数据库·mysql
RobinXYuan16 小时前
【Java 字节码探秘】02 ClassFile 骨架:十个字段
java·jvm·源码阅读
炸薯条!16 小时前
Python面向对象基础:类与对象、实例方法、魔法方法和属性详解
java·开发语言·python
SimonKing17 小时前
又一款数据库同步神器,2.3K Star的dbswitch
java·后端·程序员
CoderYanger17 小时前
Java EE 进阶:11. SpringBoot 统一功能处理(第二弹):DispatcherServlet 源码分析+适配器模式
java·数据库·hive·spring boot·面试·java-ee·mybatis
Rain的Java大神之路18 小时前
前端转 Java 全栈:用 AI 两天交付一个能跑的 Demo,踩坑全记录
java·前端·后端