M
M
mostro122016-07-14 19:45:59
Java
mostro12, 2016-07-14 19:45:59

How to work with date in Java?

You need to enter the date in the class fields in the format DD MM YYYY HH MM SS, and then perform logical calculations with dates (Example 01/01/2016. 00:00:00 > 01/01/2013 00:00:00 ? ) Tell me how you can implement this task ?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Alexey Cheremisin, 2016-07-14
@leahch

For the eighth java - the code is below. For everyone else - joda.org

package jtests;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;

public class MyTest3 {

  public static void main(String[] args) {
    DateTimeFormatter fmt = new DateTimeFormatterBuilder()
        .appendPattern("dd.MM.yyyy. HH:mm:ss")
        .toFormatter();
    
    LocalDateTime t1 = LocalDateTime.parse("01.01.2016. 00:00:00", fmt);
    LocalDateTime t2 = LocalDateTime.parse("01.01.2013. 00:00:00", fmt);
    
    System.out.println(t1);
    System.out.println(t2);
    System.out.println(t1.isAfter(t2));
    System.out.println(t2.isAfter(t1));
  }
}

K
Konstantin Malyarov, 2016-07-14
@Konstantin18ko

Use Date, and the methods associated with that type.

D
Dmitry Serkov, 2016-07-14
@Hutaab

import java.time.*;
LocalDateTime date1 = LocalDateTime.of(2016, Month.JULY, 14, 20, 25);
LocalDateTime date2 = LocalDateTime.of(2016, Month.JULY, 13, 20, 25);
date1.compareTo( date2 )
The compareTo() method returns:
0 - if both instances are equal;
1 - if the date, whose method is called, is after the date, which enters the method as a parameter;
-1 - if the date whose method is called is before the date that is passed to the method as a parameter.
The isAfter() method returns true ONLY when the date whose method is being called is AFTER the date that is passed as a parameter to the method. That is, if the compareTo() method is executed for the same objects, it will return 1.
The isBefore() method returns true ONLY when the date whose method is called is BEFORE the date that is passed as a parameter to the method. That is, if the compareTo() method is executed for the same objects, it will return -1.
The isEqual() method returns true if both dates are the same.

A
aol-nnov, 2016-07-14
@aol-nnov

I will be original: joda time :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question