摘篇别人总结的关于日期的东西

java中几个常见的关于日期的问题

1. 怎样计算两个时间之间的间隔?
间隔=Date1.getTime()-Date2.getTime();得出来的是毫秒数.
除1000是秒,再除60是分,再除60是小时..............................

记住java标准库中所有时间类都以此为基础转化的,只是他写好了一些
转化的方法给你用而已.但都离不开这个毫秒数为基础.

2. t=Calendar.getInstance();m=t.get(t.MONTH)+1;这里为什么要加一?

在java语言里,date的month的取值范围是:0~11,与人们的自然表达上相差1。


3. 系统时间与当前日期的区别?

系统时间确切的说应该是
System.currentTimeMillis();
new Date()是当前日期,虽然它getTime();和System.currentTimeMillis();
一样,但System.currentTimeMillis();


4. 如何计算两个日期的天数差值?

long beginTime = beginDate.getTime();
long endTime2 = endDate.getTime();
long betweenDays = (long)((endTime - beginTime) / (1000 * 60 * 60 *24) + 0.5); 

 

5. 如何比较日期时间大小?

第一种方法:
use Calendar object to compare
java.util.Calendar class can be used to compare date. In order to do this,
you guy should parse that string into int year, month, day and construct a
Calendar object, and then do comparison.

Below is a sample

StringTokenizer token = new StringTokenizer(your string,"-");
int year = Integer.parseInt(token.nextToken());
int month = Integer.parseInt(token.nextToken());
int day = Integer.parseInt(token.nextToken());
Calendar date = Calendar.getInstance();
date.set(year,month,day);
Calendar today = Calendar.getInstacne();
if(date.after(today)){
//......
} 
 


第二种方法

Date nowDate=new Date();//当前时间\r
long nowTime=nowDate.getTime;
long lastTime=userTime.longValue();//以前的时间\r
long time=nowTime-lastTime;//时间相减比较。
if(time>(long)60000)//1分钟{} 

 

另外可用以下参考
用时间戳,Date.getTime()可以把当前时间改成时间戳,
用CompareTo();
用before(),after(),equals();

6. 格式化日期的问题\r

目的:
第一次求日期

java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyyy-MM-dd");
String riqi=formatter.format(currentTime_1); 
 


第二次求时间\r

java.text.DateFormat format1 = new java.text.SimpleDateFormat("hhmmss");
java.util.Date currentTime_2 = new java.util.Date();
String shijian=format1.format(currentTime_2); 
 


得到的结果是
2002-02-19和115324(11点53分24秒)

实现:

java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyyy-MM-dd-H-mm-ss");
java.util.Date currentTime_1 = new java.util.Date();
String str_date = formatter.format(currentTime_1);
StringTokenizer token = new StringTokenizer(str_date,"-");
String year = token.nextToken();
String month= token.nextToken();
String day = token.nextToken();
String hh = token.nextToken();
String mm = token.nextToken();
String ss = token.nextToken();
String riqi=year+"年\\"+month+"月"+day+"日"+" "+hh+"点\\"+mm+"分"+ss+"秒\\";
String newdir=year+month+day;
String wenjian = hh+mm+ss; 
 



7. 怎么得到一个月的天数?

java.util.Calendar date = java.util.Calendar.getInstance();
System.out.println(date.getActualMaximum(date.DAY_OF_MONTH));

 



8.日期转化代码(直接复制就可用)

import java.util.*;
import java.text.*;
class Test2
{
public static void main(String[] args)
{
  
  String d1="2008-03-01 00:34";  //到期
        String d2="2008-02-27 11:33";  //现在时间
        Date da1=getDate(d1);
        Date da2=getDate(d2);
        long time=(long)((da1.getTime()-da2.getTime())/(1000*60*60*24)+0.5);
  int hour1= Integer.parseInt(d1.substring(11,13));
  int minute1= Integer.parseInt(d1.substring(14,16));
  int hour2= Integer.parseInt(d2.substring(11,13));
  int minute2= Integer.parseInt(d2.substring(14,16));
  int hour =0;
  int minute =0;
  hour = Math.abs(hour1-hour2);
  if (minute1 < minute2) {
   minute = 60 + minute1 - minute2;
   hour1--;
  } else {
   minute = minute1 - minute2;
  }
  if (hour1 < hour2) {
   hour = 24 + hour1 - hour2;
  } else {
   hour = hour1 - hour2;
  }
        System.out.println(time+"天"+ hour + "小时" + minute+"分");
}
public static Date getDate(String date) {
        Date datestr = null;
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
  try{
   datestr = sf.parse(date);
  } catch(Exception e){
  }
        return datestr;
}
// SimpleDateFormate   sdf   =   new   SimpleDateFormat("yyyy-MM-dd   HH:mm:ss"); 
//  Date   date1   =   sdf.parse(dateString1); 
//  Date   date2   =   sdf.parse(dateString2); 
  
//  int   diff   =   (date1.getTime()   -   date2.getTime())/(1000*3600*24);
} 
 

 

你可能感兴趣的:(java)