zoukankan      html  css  js  c++  java
  • timestamp、yyyy-MM-dd HH:mm:ss互相转换

    0.记得加上T、.422+0800

    1. 传值yyyy-MM-dd格式, Date接收默认为8:00
    • "date": "2021-06-30"
    • Wed Jun 30 08:00:00 CST 2021
    • Wed Jun 30 08:00:00 CST 2021
    • 2021-06-30 08:00:00
    • Wed Jun 30 08:00:00 CST 2021

    1. 自定义时间, Date接收自动转换
    @PostMapping("/PasadenaDate")
    public void PasadenaDate(@RequestBody StudentPlus studentPlus) throws ParseException {
      DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
      // POST请求入参: "date": "2021-06-30T23:59:59.422+0800"
    
      // 接收Date类型默认参数
      // Wed Jun 30 23:59:59 CST 2021
      System.out.println(studentPlus.getDate());
    
      // JSON转换后的Date类型默认参数
      // Wed Jun 30 23:59:59 CST 2021
      System.out.println(JSON.toJSON(studentPlus.getDate()));
    
      // Date默认参数 转换 为yyyy-MM-dd HH:mm:ss格式
      // 2021-06-30 23:59:59
      System.out.println(dateFormat.format(studentPlus.getDate()));
    
      // yyyy-MM-dd HH:mm:ss 转换 为Date默认格式参数
      // Wed Jun 30 23:59:59 CST 2021
      System.out.println(dateFormat.parse(dateFormat.format(studentPlus.getDate())));
    }
    

    3.数据库为timestamp类型, Java程序中为Date类型会展示

    • 原始
      • 2021-06-30T08:27:30.000+0000
    • 格式化, 实体类时间字段加注解: @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
      • 展示: 2021-06-30 16:27:30

    1. 方法二, 将timestamp类型 转换为 yyyy-MM-dd HH:mm:ss
        String a = "2021-05-14T23:30:35+08:00";
        LocalDateTime date = LocalDateTime.parse(a, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
        String dateString = date.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        System.out.println("dateString:" + dateString);
        // TODO: dateString:2021-05-14 23:30:35
    

    1. yyyy-MM-dd HH:mm:ss格式 转换为 timestamp类型

  • 相关阅读:
    Python 使用正则表达式匹配URL网址
    第3章 网络爬虫基础
    《精通Python网络爬虫》
    /etc/hosts
    Linux alias 命令
    file()
    Win10 取消桌面快捷键图标
    Win10 我的电脑 -- 右键点击管理打不开
    MongoDB 备份恢复
    ORACLE 日期比较
  • 原文地址:https://www.cnblogs.com/Twittery/p/14955529.html
Copyright © 2011-2022 走看看