zoukankan      html  css  js  c++  java
  • 日期 JAVA8 LocalDateTime

    LocalDateTime <=> String

    //时间转字符串格式化
    DateTimeFormatter df1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
    String dateTime = LocalDateTime.now(ZoneOffset.of("+8")).format(df1);
    
    //字符串转时间
    String dateTimeStr = "2020-07-07 13:14:52.999";
    DateTimeFormatter df2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
    LocalDateTime dateTime = LocalDateTime.parse(dateTimeStr, df2);

      

    LocalDateTime获取毫秒数

    //获取秒数
    Long second = LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"));
    //获取毫秒数
    Long milliSecond = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();

    String型日期从一种格式转换成另一种格式的String型日期

        /**
         * String型日期从一种格式转换成指定格式的String型日期   */
        public static String changeFormat(String strDate, String inputFormat, String outputFormat) throws ParseException {
            if (strDate == null || strDate.length() <= 0) {
                throw new IllegalArgumentException("invalid date = " + strDate);
            }
            DateTimeFormatter inDTF = DateTimeFormatter.ofPattern(inputFormat);
            DateTimeFormatter outDTF = DateTimeFormatter.ofPattern(outputFormat);
            
            LocalDateTime ldt = LocalDateTime.parse(strDate, inDTF);
            return outDTF.format(ldt);
        }

    JDK8以前

        @Test
        public void testDate() {
            /** "yyyy-MM-dd HH:mm:ss.S" */
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
            /** GET system DateTime */
            String sysDate = sdf.format(new Timestamp(System.currentTimeMillis()));
    
            String year = "2021";
            String month = "3";
            String day = "5";
            String hour = "15";
            String minute = "50";
            String second = "20";
            int millisecond = 996;
    
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.YEAR, Integer.parseInt(year));
            calendar.set(Calendar.MONTH, Integer.parseInt(month) - 1); /* month start as 0 */
            calendar.set(Calendar.DATE, Integer.parseInt(day));
            calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hour));
            calendar.set(Calendar.MINUTE, Integer.parseInt(minute));
            calendar.set(Calendar.SECOND, Integer.parseInt(second));
            calendar.set(Calendar.MILLISECOND, millisecond);
            Date returnDate = calendar.getTime();
    
            String strDate = sdf.format(new Timestamp(returnDate.getTime()));
    
            System.out.println("sysDate [" + sysDate);
            System.out.println("strDate [" + strDate);
        }

      

    作者: BORS

  • 相关阅读:
    Java for循环打印九九乘法表
    java判断回文数代码实例
    java 判断回文数实例代码
    价值
    Java代码实例 判断这个数是否是素数
    Java for循环求水花仙数
    java代码实例 使用switch实现简易的计算器(实现加减乘除)
    java基础代码实例 求1100之间的奇数和偶数
    Java 判断闰年代码实例
    可以将控制台输出信息保存到本地的Log4j配置
  • 原文地址:https://www.cnblogs.com/bors/p/dateTransform.html
Copyright © 2011-2022 走看看