最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
java DateFormat日期格式化详解
时间:2011-05-09 编辑:简简单单 来源:一聚教程网
DateFormat支持风格或形状,你可以有四种不同类型格式日期对象。参考下表
Value | Example |
DateFormat.SHORT. | For example, 12/2/05 |
DateFormat.MEDIUM. | For example, Dec 2, 2005. |
DateFormat.LONG. | For example, December 2, 2005 |
DateFormat.FULL. | For example, Friday, December 2, 2005 |
看一个简单的输出实例
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;public class Main {
public static void main(String[] argv) throws Exception {Format formatter = new SimpleDateFormat("HH:mm:ss Z", Locale.CANADA);
Date date = (Date) formatter.parseObject("21:44:07 Heure normale du Pacifique");
}
}
上面是一个非常简单的普通的日期格式化,下面实例缩合了上面的表单。
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;public class MainClass {
public static void main(String[] args) {
DateFormat shortDf = DateFormat.getDateInstance(DateFormat.SHORT);DateFormat mediumDf = DateFormat.getDateInstance(DateFormat.MEDIUM);
DateFormat longDf = DateFormat.getDateInstance(DateFormat.LONG);
DateFormat fullDf = DateFormat.getDateInstance(DateFormat.FULL);
System.out.println(shortDf.format(new Date()));
System.out.println(mediumDf.format(new Date()));
System.out.println(longDf.format(new Date()));
System.out.println(fullDf.format(new Date()));// parsing
try {
Date date = shortDf.parse("12/12/2006");
} catch (ParseException e) {
}
}
}
-
上一个: java join字符串连接用法
-
下一个: Servlet缓冲Buffer应用实例