提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
使用自定义格式格式化日期
/*时 代 J a v a - N o w J a v a . c o m 提供*/ import java.text.Format; import java.text.SimpleDateFormat; import java.util.Date; public class Main { public static void main(String[] argv) throws Exception { Format formatter; // The year formatter = new SimpleDateFormat("yy"); // 02 formatter = new SimpleDateFormat("yyyy"); // 2002 // The month formatter = new SimpleDateFormat("M"); // 1 formatter = new SimpleDateFormat("MM"); // 01 formatter = new SimpleDateFormat("MMM"); // Jan /* 来 自* nowjava - 时代Java */ formatter = new SimpleDateFormat("MMMM"); // January // The day formatter = new SimpleDateFormat("d"); // 9 formatter = new SimpleDateFormat("dd"); // 09 // The day in week formatter = new SimpleDateFormat("E"); // Wed formatter = new SimpleDateFormat("EEEE"); // Wednesday // Get today's date Date date = new Date(); // Some examples formatter = new SimpleDateFormat("MM/dd/yy"); String s = formatter.format(date); System.out.println(s); formatter = new SimpleDateFormat("dd-MMM-yy"); s = formatter.format(date); System.out.println(s);