1. 程式人生 > >轉換日期格式的工具類

轉換日期格式的工具類

ring mage code edate print util println pan date()

寫一個工具類,用來轉換固定的日期格式:

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

public class DateFormat {
    public static String convert(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dateStr = sdf.format(date);
        return dateStr;
    }
}

輸出時:

import java.util.Date;

public class test {
      public static void main(String[] args) {
          Date date = new Date();
         System.out.println(DateFormat.convert(date)); 
    }
}

輸出效果為:

技術分享

轉換日期格式的工具類