4 ways to get the current time in java

1、Date day=new Date();    

[SimpleDateFormat] df = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”); 

System.out.println(df.format(day));   

Get the current time     through the [Date class]

2、SimpleDateFormat df = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);   System.out.println(df.format(System.currentTimeMillis()));   

Get the current time through the currentTimeMillis method in the System class 

  1. Calendar c = Calendar.getInstance();//You can modify each time domain separately   

int year = c.get(Calendar.YEAR);  

int month = c.get(Calendar.MONTH);   

int date = c.get(Calendar.DATE);    

int hour = c.get(Calendar.HOUR_OF_DAY);   

int minute = c.get(Calendar.MINUTE);   

int second = c.get(Calendar.SECOND);    

System.out.println(year + “/” + month + “/” + date + ” ” +hour + “:” +minute + “:” + second);    

Get the current time through the Calendar class   

4、 Date date = new Date();    

String year = String.format(“%tY”, date);   

String month = String.format(“%tB”, date);   

String day = String.format(“%te”, date);    

System.out.println(“Today is:”+year+”-“+month+”-“+day);   

Get the current time through the Date class   

Summary: Setting the time format can be set by calling the SimpleDateFormat class and by the format method in String. Available through Date class and currentTimeMillis in System!

</div>

Leave a Comment

Your email address will not be published. Required fields are marked *