-1
public class Output extends javax.swing.JFrame {

    public Output() {
        initComponents();
        setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("abc.jpg")));
        setSize(400, 700);
        setLocationRelativeTo(null );
        setResizable(false);
        setdate();
    }
    public void setdate(){
        ActionListener obj = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                java.util.Date date = new Date();

                Date.setText(date.getDate()+"-"+(1+date.getMonth())+
                        "-"+date.getYear()+" / "+date.getHours() + ":" 
                        + date.getMinutes()+ ":" + 
                        date.getSeconds());
            }
        };
        new javax.swing.Timer(1000,obj).start();       
}

Whenever I run this code I get every thing OK but while setting year it shows 18-8-116, while it should show 18-8-16 or 18-8-2016.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • You are using a troublesome old class now supplanted by [`java.time.LocalDate`](http://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html). Instead call [`LocalDate.of( int year , int month , int dayOfMonth )`](http://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html#of-int-int-int-). No funky counting on this class; the year 2016 is `2016` as it should be. – Basil Bourque Aug 07 '16 at 17:00

1 Answers1

1

Date stores the year of a date as years from 1900, hence the 116 (1900 + 116 = 2016).

Most of Date's methods have been deprecated since Java 1.1. Until Java 1.8, you better used Calendar or LocalDate from Joda-Time. Since Java 1.8, Date has been superseded by LocalDate, LocalTime, LocalDateTime and their timezoned counterparts, i.e. ZonedDateTime.

So you might want to use a LocalDate here, i.e.

LocalDate date = LocalDate.of(2016, Month.AUGUST, 18);
Rudolf Held
  • 503
  • 3
  • 7
  • 1
    Only certain "methods in[`Date`](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html) are deprecated." – trashgod Aug 07 '16 at 12:59
  • 1
    oops, yes, it's true. The class Date is not deprecated, but the majority of it's methods. I have corrected that. – Rudolf Held Aug 07 '16 at 14:46
  • While not entirely deprecated, the class Date is supplanted by the [java.time](http://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) classes as mentioned in the class documentation. – Basil Bourque Aug 07 '16 at 16:57