zoukankan      html  css  js  c++  java
  • SimpleDateFormat 和 LocalDate、LocalTime 以及时间大小比较简单示例

    package mytest;
    
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.time.DayOfWeek;
    import java.time.LocalDate;
    import java.time.temporal.TemporalAdjusters;
    import java.util.Date;
    
    public class MyDateFormatTest {
        public static void main(String[] args) throws ParseException {
            //myLocalDateTest();
            timeCompare();
        }
        // 方法 一
        public void mySimpleDateFormat() {
            Date d = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String timeString = sdf.format(d);// foemat() date 转 String
            System.out.println(timeString);
            try {
                Date d2 = sdf.parse("2019-02-26 23:03:46");// prase() String 转date
                System.out.println(d2);
    
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
    
        // 方法 二
        public static void myLocalDateTest() {
            /**
             * localDate
             */
            // 取当前日期:
            LocalDate today = LocalDate.now(); // -> 2014-12-24
            // 根据年月日取日期:
            LocalDate crischristmas = LocalDate.of(2014, 12, 25); // -> 2014-12-25
            // 根据字符串取:
            LocalDate endOfFeb = LocalDate.parse("2014-02-28"); // 严格按照ISO
                                                                // yyyy-MM-dd验证,02写成2都不行,当然也有一个重载方法允许自己定义格式
            LocalDate.parse("2014-02-29"); // 无效日期无法通过:DateTimeParseException:
                                            // Invalid date
            /**
             * localTime
             */
            // 取本月第1天:
            LocalDate firstDayOfThisMonth = today.with(TemporalAdjusters.firstDayOfMonth()); // 2017-03-01
            // 取本月第2天:
            LocalDate secondDayOfThisMonth = today.withDayOfMonth(2); // 2017-03-02
            // 取本月最后一天,再也不用计算是28,29,30还是31:
            LocalDate lastDayOfThisMonth = today.with(TemporalAdjusters.lastDayOfMonth()); // 2017-12-31
            // 取下一天:
            LocalDate firstDayOf2015 = lastDayOfThisMonth.plusDays(1); // 变成了2018-01-01
            // 取2017年1月第一个周一,用Calendar要死掉很多脑细胞:
            LocalDate firstMondayOf2015 = LocalDate.parse("2017-01-01").with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY)); // 2017-01-02
        }
        public static void timeCompare() throws ParseException{
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date d1 = sdf.parse("2018-10-15 11:11:11");
            Date d2 = sdf.parse("2020-10-15 11:11:11");
            
            System.out.println(date.after(d1));//date 在 d1 之后吗?
            System.out.println(date.before(d2));//date 在d2之前吗?
            
        }
    
    }
  • 相关阅读:
    IXmlSerializable With WCFData Transfer in Service Contracts
    Difference Between XmlSerialization and BinarySerialization
    Using XmlSerializer (using Attributes like XmlElement , XmlAttribute etc ) Data Transfer in Service Contracts
    Introducing XML Serialization
    Version Tolerant Serialization
    Which binding is bestWCF Bindings
    Data Transfer in Service Contracts
    DataContract KnownTypeData Transfer in Service Contracts
    Using the Message ClassData Transfer in Service Contracts
    DataContract POCO SupportData Transfer in Service Contracts
  • 原文地址:https://www.cnblogs.com/zhangheliang/p/10441007.html
Copyright © 2011-2022 走看看