Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2043494
  • 博文数量: 519
  • 博客积分: 10070
  • 博客等级: 上将
  • 技术积分: 3985
  • 用 户 组: 普通用户
  • 注册时间: 2006-05-29 14:05
个人简介

只问耕耘

文章分类

全部博文(519)

文章存档

2016年(1)

2013年(5)

2011年(46)

2010年(220)

2009年(51)

2008年(39)

2007年(141)

2006年(16)

我的朋友

分类: Java

2010-06-08 15:20:09

SimpleDateFormat is a concrete class for formatting and parsing dates in a locale-sensitive manner. It allows for formatting (date -> text), parsing (text -> date), and normalization.
SimpleDateFormat allows you to start by choosing any user-defined patterns for date-time formatting. However, you are encouraged to create a date-time formatter with either getTimeInstance, getDateInstance, or getDateTimeInstance in DateFormat. Each of these class methods can return a date/time formatter initialized with a default format pattern. You may modify the format pattern using the applyPattern methods as desired. For more information on using these methods, see DateFormat.
package c1;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test1 { 
 

  public static void main(String[] args) {
 
     SimpleDateFormat sdf = new SimpleDateFormat ("MM/dd/yyyy HH:mm:ss");
     SimpleDateFormat sdf1 = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
     //日期转换成字符串
     String str=sdf.format(new Date());
     System.out.println(str);
    
     //   字符串转换成日期
     try {
   Date d1=sdf.parse(str);
   String str1=sdf1.format(d1);
   System.out.println(str1);
  } catch (ParseException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  }
}
Output:
06/08/2010 15:08:22
2010-06-08 15:08:22
阅读(1030) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~