当前位置:编程学习 > JSP >>

java DateFormat日期格式化详解

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) {
    }
  }
}

补充:Jsp教程,Java基础 
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,