源代码如下:
import java.text.SimpleDateFormat;
import java.util.Date;
public class TimeDemo {//获取现在时间并且将时间进行格式化
public static void main(String[] args){
Date date = new Date();
System.out.println(date);
//将时间进行格式化
//yyyy年MM月dd日 HH:mm:ss格式
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
String time1 = sdf1.format(date);
System.out.println(time1);
//yyyy/MM/dd HH:mm:ss格式
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
String time2 = sdf2.format(date);
System.out.println(time2);
//yyyy-MM-dd HH:mm:ss格式
SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time3 = sdf3.format(date);
System.out.println(time3);
}
}
运行效果如图所示: