java中Date的用法

作者在 2006-12-10 03:27:00 发布以下内容

//示例1

import java.util.Date;
import java.util.Calendar;
public class DateDemo
{
/**
 *获取当前时间
 */
 public static void main(String[] args)
 {  
  
  System.out.println("今天最早时刻 = "+getBeginTime());
  System.out.println("今天最晚时刻 = "+getEndTime());
 }
 public static Date getBeginTime()
 {
  Calendar rightNow=Calendar.getInstance();
  //rightNow.setTime(date);
  rightNow.set(Calendar.HOUR_OF_DAY,23);
  rightNow.set(Calendar.MINUTE,59);
  rightNow.set(Calendar.SECOND,59);
  
  return rightNow.getTime();
 }
 public static Date getEndTime()
 {
  Calendar rightNow=Calendar.getInstance();
  //rightNow.setTime(date);
  rightNow.set(Calendar.HOUR_OF_DAY,0);
  rightNow.set(Calendar.MINUTE,0);
  rightNow.set(Calendar.SECOND,0);
  
  return rightNow.getTime();
 }
}

//示例2

import java.util.Date;
import java.util.Calendar;
public class CalendarDemo
{
/**
 *获取当前时间
 */
 public static void main(String[] args)
 {  
  Calendar obj=Calendar.getInstance();
  
  System.out.println("Date 和 Time的各个组成部分 : ");
  
  System.out.println("年:"+obj.get(Calendar.YEAR));
  System.out.println("月:"+obj.get(Calendar.MONTH));
  System.out.println("日:"+obj.get(Calendar.DATE));
  System.out.println("小时:"+obj.get(Calendar.HOUR));
  System.out.println("分钟:"+obj.get(Calendar.MINUTE));
  System.out.println("秒:"+obj.get(Calendar.SECOND));
  
  System.out.println("现在时刻:"+obj.getTime());
  //
  obj.add(Calendar.MINUTE,30);
  
  System.out.println("再过30分钟时刻:"+obj.getTime());
 }
}

默认分类 | 阅读 4830 次
文章评论,共0条
游客请输入验证码
文章分类
最新评论