java日期代码详解

作者:操作系统

java时间日期使用与查询代码详解,java日期代码详解

一经格式精确,直接比较字符串就足以了呀,准确到秒的也一样

String  s1  =  "2003-12-12  11:30:24";  
 String  s2  =  "2004-04-01  13:31:40";  
 int  res  =  s1.compareTo(s2);  

求日期差

SimpleDateFormat  df  =  new  SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");  
 Date  begin=df.parse("2004-01-02  11:30:24");  
 Date  end  =  df.parse("2004-03-26  13:31:40");  
 long  between=(end.getTime()-begin.getTime())/1000;//除以1000是为了转换成秒  
 int  day=between/(24*3600);  
 int  hour=between%(24*3600)/3600;  
 int  minute=between%3600/60;  
 int  second=between%60; 

不常用到时间日期类,所以就将常用的日子方法和品质都归咎总括如下,方便我们找找

1.计量某四月份的最流年气

Calendar time=Calendar.getInstance(); 
time.clear(); 
time.set(Calendar.YEAR,year); 
time.set(Calendar.MONTH,i-1);//注意,Calendar对象默认一月为0 
int day=time.getActualMaximum(Calendar.DAY_OF_MONTH);//本月份的天数 

注:在动用set方法以前,最棒先clear一下,不然广大消息会持续自系统当下日子

2.Calendar和Date的转化

(1) Calendar转化为Date

Calendar cal=Calendar.getInstance(); 
Date date=cal.getTime(); 

(2) Date转化为Calendar

Date date=new Date(); 
Calendar cal=Calendar.getInstance(); 
cal.setTime(date); 

3.格式化输出日期时间

Date date=new Date(); 
SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); 
System.out.println(df.format(date)); 

4.总结一年中的第几礼拜

(1)总括某一天是一年中的第几星期

Calendar cal=Calendar.getInstance(); 
cal.set(Calendar.YEAR, 2006); 
cal.set(Calendar.MONTH, 9); 
cal.set(Calendar.DAY_OF_MONTH, 3); 
int weekno=cal.get(Calendar.WEEK_OF_YEAR); 

(2)总结一年中的第几星期是几号

SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd"); 
Calendar cal=Calendar.getInstance(); 
cal.set(Calendar.YEAR, 2006); 
cal.set(Calendar.WEEK_OF_YEAR, 1); 
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); 
System.out.println(df.format(cal.getTime())); 

输出:

2006-01-02 

5.add()和roll()的用法
(1)add()方法

SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd"); 
Calendar cal=Calendar.getInstance(); 
cal.set(Calendar.YEAR, 2006); 
cal.set(Calendar.MONTH, 9); 
cal.set(Calendar.DAY_OF_MONTH, 3); 
cal.add(Calendar.DATE, -4); 
Date date=cal.getTime(); 
System.out.println(df.format(date)); 
cal.add(Calendar.DATE, 4); 
date=cal.getTime(); 
System.out.println(df.format(date)); 

输出:
2006-08-30
2006-10-03

(2)roll方法

cal.set(Calendar.YEAR, 2006); 
cal.set(Calendar.MONTH, 9); 
cal.set(Calendar.DAY_OF_MONTH, 3); 
cal.roll(Calendar.DATE, -4); 
date=cal.getTime(); 
System.out.println(df.format(date)); 
cal.roll(Calendar.DATE, 4); 
date=cal.getTime(); 
System.out.println(df.format(date)); 

输出:

2006-10-29

2006-10-03

足见,roll()方法在当月内循环,平日采取add()方法;

6.总括三个随机时间中间的间隔天数

(1)传进Calendar对象

/** */
/**计算两个时间之间相隔天数 
* @param startday 开始时间 
* @param endday 结束时间 
* @return 
*/
public int getIntervalDays(Calendar startday,Calendar endday)...{
 //确保startday在endday之前 
 if(startday.after(endday))...{
  Calendar cal=startday;
  startday=endday;
  endday=cal;
 }
 //分别得到两个时间的毫秒数 
 long sl=startday.getTimeInMillis();
 long el=endday.getTimeInMillis();
 long ei=el-sl;
 //根据毫秒数计算间隔天数 
 return (int)(ei/(1000*60*60*24));
}

(2)传进Date对象

/** */
/**计算两个时间之间相隔天数 
* @param startday 开始时间 
* @param endday 结束时间 
* @return 
*/
public int getIntervalDays(Date startday,Date endday)...{
 //确保startday在endday之前 
 if(startday.after(endday))...{
  Date cal=startday;
  startday=endday;
  endday=cal;
 }
 //分别得到两个时间的毫秒数 
 long sl=startday.getTime();
 long el=endday.getTime();
 long ei=el-sl;
 //根据毫秒数计算间隔天数 
 return (int)(ei/(1000*60*60*24));
}

同理,可以用一样的办法总括出自便多个时刻相隔的时辰数,分钟数,秒钟数等
注:以上措施是截然定期间估测计算,有的时候并不能够快心满志,如:

startday="2006-10-11 20:00:00" 
endday="2006-10-12 8:00:00" 

计量结果为0,然则大家恐怕相让计算结果形成1,此时能够用如下方法达成:
在传参此前,先设定endday的时光,如:

endday.set(Calendar.HOUR_OF_DAY, 23); 
endday.set(Calendar.MINUTE, 59); 
endday.set(Calendar.SECOND, 59); 
endday.set(Calendar.MILLISECOND, 59); 

如此那般再传进去startday,endday,则结果就如我们所愿了。然而,假设嫌以上措施行麻醉烦,能够参谋以下方法:
(3)创新标准总括相隔天数的点子

public int getDaysBetween (Calendar d1, Calendar d2) ...{
 if (d1.after(d2)) ...{
  // swap dates so that d1 is start and d2 is end 
  java.util.Calendar swap = d1;
  d1 = d2;
  d2 = swap;
 }
 int days = d2.get(Calendar.DAY_OF_YEAR) - d1.get(Calendar.DAY_OF_YEAR);
 int y2 = d2.get(Calendar.YEAR);
 if (d1.get(Calendar.YEAR) != y2) ...{
  d1 = (Calendar) d1.clone();
  do ...{
   days += d1.getActualMaximum(Calendar.DAY_OF_YEAR);
   //得到当年的实际天数 
   d1.add(Calendar.YEAR, 1);
  }
  while (d1.get(Calendar.YEAR) != y2);
 }
 return days;
}

收获系统当下岁月:

public static String getSystemTime(){
 Date date=new Date();
 SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 return df.format(date);
}
//字符串转化成时间类型(字符串可以是任意类型,只要和SimpleDateFormat中的格式一致即可) 
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("M/dd/yyyy hh:mm:ss a",java.util.Locale.US);
java.util.Date d = sdf.parse("5/13/2003 10:31:37 AM");
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String mDateTime1=formatter.format(d);
//当前时间 
Calendar cal = Calendar.getInstance();
// SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss G E D F w W a E F");
String mDateTime=formatter.format(cal.getTime());
//1年前日期 
java.util.Date myDate=new java.util.Date();
long myTime=(myDate.getTime()/1000)-60*60*24*365;
myDate.setTime(myTime*1000);
String mDate=formatter.format(myDate);
//明天日期 
myDate=new java.util.Date();
myTime=(myDate.getTime()/1000)+60*60*24;
myDate.setTime(myTime*1000);
mDate=formatter.format(myDate);
//两个时间之间的天数 
SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date date= myFormatter.parse("2003-05-1");
java.util.Date mydate= myFormatter.parse("1899-12-30");
long day=(date.getTime()-mydate.getTime())/(24*60*60*1000);
//加半小时 
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
java.util.Date date1 = format.parse("2002-02-28 23:16:00");
long Time=(date1.getTime()/1000)+60*30;
date1.setTime(Time*1000);
String mydate1=formatter.format(date1);
//年月周求日期 
SimpleDateFormat formatter2 = new SimpleDateFormat("yyyy-MM F E");
java.util.Date date2= formatter2.parse("2003-05 5 星期五");
SimpleDateFormat formatter3 = new SimpleDateFormat("yyyy-MM-dd");
String mydate2=formatter3.format(date2);
//求是星期几 
mydate= myFormatter.parse("2001-1-1");
SimpleDateFormat formatter4 = new SimpleDateFormat("E");
String mydate3=formatter4.format(mydate);

在付出web应用中,针对分歧的数据库日期类型,大家供给在我们的主次中对日期类型做各个区别的改变。若对应数据库数据是oracle的Date类型,即只须求年月日的,能够选择使用java.sql.Date类型,若对应的是MSsqlserver数据库的DateTime类型,即必要年月日时分秒的,选用java.sql.Timestamp类型

您能够采取dateFormat定义时间日期的格式,转二个字符串就可以

package personal.jessica;
import java.util.Date;
import java.util.Calendar;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Locale;
class Datetest{
 /** 
*method 将字符串类型的日期转换为一个timestamp(时间戳记java.sql.Timestamp) 
*@param dateString 需要转换为timestamp的字符串 
*@return dataTime timestamp 
*/
 public final static java.sql.Timestamp string2Time(String dateString)  
 throws java.text.ParseException {
  DateFormat dateFormat;
  dateFormat = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss.SSS", Locale.ENGLISH);
  //设定格式  
  //dateFormat = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss", Locale.ENGLISH);  
  dateFormat.setLenient(false);
  java.util.Date timeDate = dateFormat.parse(dateString);
  //util类型  
  java.sql.Timestamp dateTime = new java.sql.Timestamp(timeDate.getTime());
  //Timestamp类型,timeDate.getTime()返回一个long型  
  return dateTime;
 }
 /** 
*method 将字符串类型的日期转换为一个Date(java.sql.Date) 
*@param dateString 需要转换为Date的字符串 
*@return dataTime Date 
*/
 public final static java.sql.Date string2Date(String dateString)  
 throws java.lang.Exception {
  DateFormat dateFormat;
  dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
  dateFormat.setLenient(false);
  java.util.Date timeDate = dateFormat.parse(dateString);
  //util类型  
  java.sql.Date dateTime = new java.sql.Date(timeDate.getTime());
  //sql类型  
  return dateTime;
 }
 public static void main(String[] args){
  Date da = new Date();
  //注意:这个地方da.getTime()得到的是一个long型的值  
  System.out.println(da.getTime());
  //由日期date转换为timestamp  
  //第一种方法:使用new Timestamp(long)  
  Timestamp t = new Timestamp(new Date().getTime());
  System.out.println(t);
  //第二种方法:使用Timestamp(int year,int month,int date,int hour,int minute,int second,int nano)  
  Timestamp tt = new Timestamp(Calendar.getInstance().get(  
  Calendar.YEAR) - 1900, Calendar.getInstance().get(  
  Calendar.MONTH), Calendar.getInstance().get(  
  Calendar.DATE), Calendar.getInstance().get(  
  Calendar.HOUR), Calendar.getInstance().get(  
  Calendar.MINUTE), Calendar.getInstance().get(  
  Calendar.SECOND), 0);
  System.out.println(tt);
  try {
   String sToDate = "2005-8-18";
   //用于转换成java.sql.Date的字符串  
   String sToTimestamp = "2005-8-18 14:21:12.123";
   //用于转换成java.sql.Timestamp的字符串  
   Date date1 = string2Date(sToDate);
   Timestamp date2 = string2Time(sToTimestamp);
   System.out.println("Date:"+date1.toString());
   //结果显示  
   System.out.println("Timestamp:"+date2.toString());
   //结果显示
  }
  catch(Exception e) {
   e.printStackTrace();
  }
 }
}

下边是例证:

Java代码

package test;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Hashtable;
import javax.swing.JOptionPane;
public class Test2{
 public static Boolean isdate(String s){
  String a[]=s.split("-");
  Boolean flg=true;
  if(!(Integer.parseint(a[0])>=1950 && Integer.parseint(a[0])<=2050)){
   flg=false;
  }
  return flg;
 }
 public static Boolean checkDate(String s){
  Boolean ret = true;
  try{
   DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
   ret = df.format(df.parse(s)).equals(s);
  }
  catch(ParseException e){
   ret = false;
  }
  return ret;
 }
 public Object dateinfo(String s){
  String a[]=s.split("-",2);
  Hashtable fest =new Hashtable();
  fest.put("01-01","元旦节");
  fest.put("02-14","情人节");
  fest.put("03-12","植树节");
  fest.put("03-15","消费者节");
  fest.put("04-01","愚人节");
  fest.put("04-05","清明节");
  fest.put("05-01","劳动节");
  fest.put("06-01","儿童节");
  fest.put("07-01","建党节");
  fest.put("08-01","建军节");
  fest.put("09-10","教师节");
  fest.put("10-01","国庆节");
  fest.put("12-25","圣诞节");
  if(fest.containsKey(a[1])){
   return fest.get(a[1]);
  } else{
   return "无节日";
  }
 }
 public String xingzuo(Date s){
  Calendar cal = Calendar.getInstance();
  cal.setTime(s);
  String xingzuo="无";
  int day=cal.get(Calendar.DAY_OF_YEAR);
  if((cal.get(Calendar.YEAR)%4==0)&&(cal.get(Calendar.YEAR)%100!=0)||(cal.get(Calendar.YEAR)%400==0)){
   if((day>=1 &&day<=19)||(day>=357&&day<=366)){
    xingzuo= "魔蝎座";
   } else if(day>=20 &&day<=49){
    xingzuo= "水瓶座";
   } else if(day>=50 &&day<=80){
    xingzuo= "双鱼座";
   } else if(day>=81 &&day<=110){
    xingzuo= "白羊座";
   } else if(day>=111 &&day<=141){
    xingzuo= "金牛座";
   } else if(day>=142 &&day<=173){
    xingzuo= "双子座";
   } else if(day>=174 &&day<=203){
    xingzuo= "巨蟹座";
   } else if(day>=204 &&day<=235){
    xingzuo= "狮子座";
   } else if(day>=236 &&day<=266){
    xingzuo= "处女座";
   } else if(day>=267 &&day<=296){
    xingzuo= "天秤座";
   } else if(day>=297 &&day<=326){
    xingzuo= "天蝎座";
   } else if(day>=327 &&day<=356){
    xingzuo= "射手座";
   }
  } else{
   if((day>=1 &&day<=19)||(day>=357&&day<=366)){
    xingzuo= "魔蝎座";
   } else if(day>=20 &&day<=48){
    xingzuo= "水瓶座";
   } else if(day>=49 &&day<=79){
    xingzuo= "双鱼座";
   } else if(day>=80 &&day<=109){
    xingzuo= "白羊座";
   } else if(day>=110 &&day<=140){
    xingzuo= "金牛座";
   } else if(day>=141 &&day<=172){
    xingzuo= "双子座";
   } else if(day>=173 &&day<=202){
    xingzuo= "巨蟹座";
   } else if(day>=203 &&day<=234){
    xingzuo= "狮子座";
   } else if(day>=235 &&day<=265){
    xingzuo= "处女座";
   } else if(day>=266 &&day<=295){
    xingzuo= "天秤座";
   } else if(day>=296 &&day<=325){
    xingzuo= "天蝎座";
   } else if(day>=326 &&day<=355){
    xingzuo= "射手座";
   }
  }
  return xingzuo;
 }
 public Date parseDate(String s){
  SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd");
  try {
   Date date3 = bartDateFormat.parse(s);
   date3=bartDateFormat.parse(s);
   return date3;
  }
  catch (Exception ex) {
   return null;
  }
 }
 public static void main(String[] args) {
  Calendar cal = Calendar.getInstance();
  Test2 test2=new Test2();
  String date1=JOptionPane.showInputDialog("请你输入日期,格式如2000-10-15");
  while(!(Test2.checkDate(date1)&&Test2.isdate(date1))){
   date1=JOptionPane.showInputDialog("请你输入日期,格式如2000-10-15");
  }
  SimpleDateFormat bartDateFormat1 = new SimpleDateFormat("yyyy,MM,dd,EEEE");
  SimpleDateFormat bartDateFormat2 = new SimpleDateFormat("yyyy-MM-dd");
  Date date2=test2.parseDate(date1);
  String festinfo=(String)test2.dateinfo(date1);
  System.out.println(bartDateFormat1.format(date2) +","+ festinfo+","+ test2.xingzuo(date2));
  String day=JOptionPane.showInputDialog("请你输入要查询N天后的日期信息");
  cal.setTime(date2);
  cal.add(Calendar.DATE, Integer.parseint(day));
  String date5=bartDateFormat2.format(cal.getTime());
  festinfo=(String)test2.dateinfo(date5);
  System.out.println(bartDateFormat1.format(cal.getTime())+","+ festinfo+","+ test2.xingzuo(cal.getTime()));
 }
}

总结

上述正是本文关于java时间日期使用与查询代码详解的全体内容,希望对大家有所帮忙。感兴趣的爱人能够延续参照本站其余相关专题,如有不足之处,招待留言提议。多谢朋友们对本站的支撑!

只要格式正确,直接相比字符串就能够了啊,正确到秒的也一样 String s1 = "二零零四-12-12 11...

本文由金冠53777-金冠娱乐53777-Welcome发布,转载请注明来源

关键词: