Java format time sample

  • 2020-04-01 03:13:44
  • OfStack

To format a date, you need to use the class java.text.dateformat

DateFormat no can directly use the constructor, generally use DateFormate subclasses - Java. Text. SimpleDateFormat complete structure.

Public SimpleDateFormat (String pattern)

The test code


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateFormatTest
{

 public static void main(String[] args)//Note :String is not written as Strings
 {
  DateFormat df = new SimpleDateFormat("yyyy years mm month dd day  hh:mm:ss");
  Date currentTime = new Date();
  String currentTimedf = df.format(currentTime);
  System.out.println(" The current time is : "+currentTimedf);
 }
}

  SimpleDateFormat has the following characteristics

1. Accept the corresponding format string and format each part of Date. Where yyyy represents year,MM represents month,DD represents day,hh represents hour,MM represents minute,ss represents second

2. In the format string, except for the part that has the representative meaning, the other part appears as is.


Related articles: