简体   繁体   中英

How to save date (dd.mm.yyyy) in java

I am getting a date in the form of dd.mm.yyyy and want to save it as a proper object. It should be comparable to another date object. How do I realize this?

使用SimpleDateFormat如下:

Date date = new SimpleDateFormat("dd/MM/yyyy").parse("12/05/2015");

Using JodaTime

String input = "03.01.2015";
DateTimeFormatter formatter = DateTimeFormat.forPattern("dd.MM.yyyy");
DateTime dt = DateTime.parse(input, formatter);
DateTime now = new DateTime();

System.out.println(dt.compareTo(now));

Use SimleDateFormat

String string = "03.01.2015";
DateFormat format = new SimpleDateFormat("MM.dd.yyyy", Locale.ENGLISH);
Date date = format.parse(string);
System.out.println(date); 

Try this code

   try {
        SimpleDateFormat sdf= new SimpleDateFormat("dd.MM.yyyy");
        Date d = sdf.parse("19.05.2090");  
        System.out.println(d);
    } catch (ParseException ex) {
       ex.printStackTrace();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM