• 【java基础】字符串和日期转化


    字符串和日期转化

    /**
     * 日期转字符串
     *
     * @author : qinyingjie
     * @date : 2022/8/3
     */
    public class Basic_01_DateToString {
        public static void main(String[] args) {
            dateToString(new Date());
        }
    
        public static String dateToString(Date date) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            System.out.println(sdf.format(date));
            sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            System.out.println(sdf.format(date));
            sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
            System.out.println(sdf.format(date));
            return sdf.toPattern();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    /**
     * 字符串转日期
     *
     * @author : qinyingjie
     * @date : 2022/8/3
     */
    public class Basic_02_StringToDate {
        public static void main(String[] args) {
            stringToDate();
        }
    
        public static void stringToDate() {
            String string = "2003-10-14 10:10:20";
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                System.out.println(sdf.parse(string));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    /**
     * 字符串转日期 字符串短了 抛异常
     *
     * @author : qinyingjie
     * @date : 2022/8/3
     */
    public class Basic_03_StringToDate {
        public static void main(String[] args) {
            stringToDate();
        }
    
        public static void stringToDate() {
            String string = "2003-10-14";
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                System.out.println(sdf.parse(string));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    /**
     * 字符串转日期 字符串长了可以解析
     *
     * @author : qinyingjie
     * @date : 2022/8/3
     */
    public class Basic_04_StringToDate {
        public static void main(String[] args) {
            stringToDate();
        }
    
        public static void stringToDate() {
            String string = "2003-10-14 10:10:20";
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            try {
                System.out.println(sdf.parse(string));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
  • 相关阅读:
    go测试库之apitest
    C语言重点语法知识
    Verilog中的系统任务(显示/打印类)--$display, $write,$strobe,$monitor
    idea中把spring boot项目打成jar包
    【AI实战】应用xgboost.XGBRegressor搭建空气质量预测模型(一)
    八大排序之快速排序
    弹性容器----六大属性(5、项目在交叉轴上的对齐方式)
    【分布式锁篇】Redisson详解
    【JavaScript】一文了解事件对象
    Linux[find命令]-根据路径和条件搜索指定文件并删除
  • 原文地址:https://blog.csdn.net/qyj19920704/article/details/126243444