java get the current time of the system and format it

java get the current time of the system and format it

CreateTime--May 9, 2018 11:41:00

Author:Marydon

There are two ways to implement

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Get the current time of the system
 * @descrition implemented using Calendar
 * @param format
 * @return
 */
public String getSysdateStr (String format) {
    Date sysdate = Calendar.getInstance().getTime();
    format = "".equals(format)?"yyyy-MM-dd HH:mm:ss":format;
    SimpleDateFormat sdf = new SimpleDateFormat(format);
    return sdf.format(sysdate);
}

/**
 * Get the current time of the system
 * @descrition is implemented using Date
 * @param format
 * @return
 */
public String getSysdateStr2 (String format) {
    Date sysdate = new Date();
    format = "".equals(format)?"yyyy-MM-dd HH:mm:ss":format;
    SimpleDateFormat sdf = new SimpleDateFormat(format);
    return sdf.format(sysdate);
}

 

 related suggestion:

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326014887&siteId=291194637