How to get year, month, day, hours, minutes, seconds and milliseconds of the current moment in Java?

后端 未结 10 1947
渐次进展
渐次进展 2020-11-29 16:58

How can I get the year, month, day, hours, minutes, seconds and milliseconds of the current moment in Java? I would like to have them as Strings.

10条回答
  •  無奈伤痛
    2020-11-29 17:14

    Or use java.sql.Timestamp. Calendar is kinda heavy,I would recommend against using it in production code. Joda is better.

    import java.sql.Timestamp;
    
    public class DateTest {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            System.out.println(new Timestamp(System.currentTimeMillis()));
        }
    }
    

提交回复
热议问题