Chinaunix首页 | 论坛 | 博客
  • 博客访问: 521756
  • 博文数量: 147
  • 博客积分: 10105
  • 博客等级: 上将
  • 技术积分: 1594
  • 用 户 组: 普通用户
  • 注册时间: 2006-06-14 10:18
文章分类

全部博文(147)

文章存档

2011年(4)

2010年(4)

2009年(6)

2008年(5)

2007年(40)

2006年(88)

我的朋友

分类: Java

2006-11-17 15:09:02

以下是一个非常詳細的关于日期的格式的demo,希望对大家有幫助!
 jeantian
import java.text.DateFormat;
import java.util.Date;
public class FormDate {
 public static void main(String[] args) {
  Date localDate=new Date();//創建一个新的本地日期对象
  Long current_time = System.currentTimeMillis();//获取系统当前时间
//  以下是5种日期的显示方式,首行必須get一种DateFormat的实例
//  系統默认日期就是medium方式,所以打出来是一样的
  DateFormat defaultDateFormat =DateFormat.getDateInstance();
  DateFormat mediumFormat = DateFormat.getDateInstance(DateFormat.MEDIUM);
//  系統简写日期
  DateFormat shortFormat = DateFormat.getDateInstance(DateFormat.SHORT);
//  系統长型日期
  DateFormat longFormat = DateFormat.getDateInstance(DateFormat.LONG);
//  系統完全显示日期
  DateFormat fullFormat = DateFormat.getDateInstance(DateFormat.FULL);
//  将获得的long time转为String
  String time=DateFormat.getInstance().format(current_time);
 
  
//      将DataFormat转化为String用于輸出
  String defaultDate = defaultDateFormat .format(localDate);
  String mediumDate = mediumFormat.format(localDate);
  String shortDate = shortFormat.format(localDate);
  String longDate = longFormat.format(localDate);
  String fullDate = fullFormat.format(localDate);
//  輸出時間
  System.out.println("系統默认日期格式============"+defaultDate);
  System.out.println("medium與系統默认日期格式一样="+mediumDate);
  System.out.println("系統简写日期================"+shortDate);
  System.out.println("系統长型日期================"+longDate);
  System.out.println("完全显示日期================"+fullDate);
  System.out.println("当前日期和时间为============="+time);
 }
}
控制台输出为
系統默认日期格式============2006-11-17
medium與系統默认日期格式一样=2006-11-17
系統简写日期================06-11-17
系統长型日期================2006年11月17日
完全显示日期================2006年11月17日 星期五
当前日期和时间为=============06-11-17 下午4:18
 
 
 
如果用import java.util.Calendar;
Calendar time=Calendar.getInstance();
Date time1=time.getTime();
System.out.println("time=============="+time1);
時間輸出为:
time==============Fri Nov 17 15:01:34 PST 2006
 
 
打印昨天的当前時間和日期如下
 
public class YesterdayCurrentTime {
 public static void main(String[] args) {
  Calendar cal=Calendar.getInstance();
  cal.add(Calendar.DATE,+2);//昨天的当前日期
  Date time = cal.getTime();//注这一句不能放上面,否则日期就是今天日期
  String currentTime=DateFormat.getInstance().format(time);//将Date轉为String顯示時間格式为06-11-21 下午2:39
//  String currentTime=DateFormat.getTimeInstance().format(time);//顯示時間格式为14:37:18 
  System.out.println(currentTime);
 }
}
 
 
 
 public static void main(String[] args) throws ParseException {
  //java中把字符串转换成日期型
      String date="2006-11-23"  ;      
         SimpleDateFormat   df   =new   SimpleDateFormat("yyyy-MM-dd");  
         java.util.Date   cDate   =   df.parse(date);  
         java.sql.Date   dd   =   new   java.sql.Date(cDate.getTime());
         System.out.println("转换后日期="+dd);
          //时间中的MM一定要大写
 }
result:
转换后日期=2006-11-23
}
 
public static void main(String[] args) {
  Date localDate=new Date();//創建一个新的本地日期对象
  SimpleDateFormat   df   =new   SimpleDateFormat("dd/MM/yyyy");
  System.out.println(df.format(localDate));
 }
结果
05/04/2007

 
阅读(1735) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~