Java date format

Java date and time API has always been something to be criticized, in order to solve this problem, Java 8 introduces new date and time API, including LocalDate, LocalTime, LocalDateTime, Clock, Instant other categories, these classes are designed are used in the same mode, so is thread-safe design.

public class DateFormatTest {

    public static void main(String[] args) {
        //通过Calendar获取日期
        Calendar calendar = Calendar.getInstance();
        Date date = calendar.getTime();

        //通过Date获取日期
        Date date1 = new Date();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("YYYY/MM/dd");
        System.out.println(simpleDateFormat1.format(date1));

        //The date format java8
         // get the date and time through the LocalDateTime 
        the LocalDateTime now = LocalDateTime.now (); 
        the DateTimeFormatter DateTimeFormatter = DateTimeFormatter.ofPattern ( "YYYY / the MM / dd HH: mm: SS" ); 
        System.out.println (now .format (DateTimeFormatter)); 
    } 
}

 

The date and time is converted to a string

public class Str2Date {
    public static void main(String[] args) {
        String dateStr = "2019年2月23号";
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd号");
        SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy/MM/dd");
        try {
            Date date = dateFormat.parse(dateStr);
            System.out.println(dateFormat2.format(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

}

 



Guess you like

Origin www.cnblogs.com/noperx/p/11361647.html