Bent
Bent

Reputation: 1385

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?

Upvotes: 2

Views: 11393

Answers (4)

Keval
Keval

Reputation: 1859

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();
    }

Upvotes: 0

planet260
planet260

Reputation: 1454

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); 

Upvotes: 1

Alan Barrows
Alan Barrows

Reputation: 599

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));

Upvotes: 1

LaurentY
LaurentY

Reputation: 7653

Use SimpleDateFormat as this:

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

Upvotes: 2

Related Questions