加载中...

测试时间对象和字符串之间的互相转换

  import java.text.DateFormat;
  import java.text.SimpleDateFormat;
  import java.util.Date;
  import java.text.ParseException;
  /**
  测试时间对象和字符串之间的互相转换
  DateFormat抽象类和SimpleDateFormat实现类的使用
  */
  public class TestDateFormat {
  public static void main(String[] args) throws ParseException {
      DateFormat df = new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss");
      //把时间对象按照“格式字符串指定的格式”转成相对于的字符串
      String str = df.format(new Date(4000000));
      System.out.println(str);    //1970年01月01日 09:06:40
  
      //把字符串按照“格式字符串指定的格式”转成相应的时间对象
      DateFormat df2 = new SimpleDateFormat("yyyy年MM月dd日 hh时mm分ss秒");
      
      Date date = df2.parse("1999年01月01日 10时10分10秒");
      System.out.println(date);   //Fri Jan 01 10:10:10 CST 1999
  }
}
posted @ 2021-08-14 11:00  nongeason  阅读(40)  评论(0)    收藏  举报