Java时间操作及格式化
在采用Java语言编程的过程中难免会遇到一些跟时间相关的操作。比如获取系统时间,对系统时间进行相应的格式化操作。
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateTest {
public static void main(String[] args) {
SimpleDateFormat format =
new SimpleDateFormat("yyyy-MM-dd-HH-mm");
/* get the current time */
long time = System.currentTimeMillis();
Date date = new Date(time);
String strDate = format.format(date);
format.applyPattern("yyyy年MM月dd日HH时mm分ss秒");
String strDate2 = format.format(date);
System.out.println(strDate);
System.out.println(strDate2);
}
}
输出:
2009-01-05-20-07
2009年01月05日20时07分15秒
yyyy-MM-dd HH:mm:ss.SSS