print current date in java

后端 未结 9 1813
半阙折子戏
半阙折子戏 2020-12-18 19:38

I want to print current date and time in java..This is the code I am trying from a java tutorial:

import java.util.*;
  
public class Date {
   public static          


        
9条回答
  •  天命终不由人
    2020-12-18 20:13

    You are getting the default toString() from Object because you created your own class named Date which is hiding the imported Date from java.util.. You can rename your class or you can use the canonical name java.util.Date like

    public static void main(String args[]) {
        java.util.Date date = new java.util.Date();
        System.out.println(date);
    }
    

    Output here is

    Mon Nov 03 10:57:45 EST 2014
    

提交回复
热议问题