java常用日期问题集锦

  • Post author:
  • Post category:java




常用日期问题集锦




1、获取服务器端当前日期:

<%@ page import=”java.util.Date”%>

<%

Date myDate = new Date();

%>



2、获取当前



、月、日:

<%@ page import=”java.util.Date”%>



<%

Date myDate = new Date();

int thisYear = myDate.getYear() + 1900;//thisYear = 2003

int thisMonth = myDate.getMonth() + 1;//thisMonth = 5

int thisDate = myDate.getDate();//thisDate = 30

%>




3、按本地时区输出当前日期

<%@ page import=”java.util.Date”%>

<%

Date myDate = new Date();

out.println(myDate.toLocaleString());

%>

输出结果为:

2003-5-30




4、获取数据库中字段名为”publish_time“、类型为Datetime的值

<%@ page import=”java.util.Date”%>

<%

…连接数据库…

ResultSet rs = …

Date sDate = rs.getDate(“publish_time”);

%>

[code]



5、按照指定格式打印日期

[code]

<%@ page import=”java.util.Date”%>

<%@ page import=”java.text.DateFormat”%>

<%

Date dNow = new Date();



SimpleDateFormat formatter = new SimpleDateFormat(“E yyyy.MM.dd ‘at’ hh:mm:ss a zzz”);

out.println(“It is ” + formatter.format(dNow));

%>



输出的结果为:

It is 星期五 2003.05.30 at 11:30:46 上午 CST

(更为详尽的格式符号请参看SimpleDateFormat类)



6、将字符串转换为日期

<%@ page import=”java.util.Date”%>

<%@ page import=”java.text.DateFormat”%>

<%

String input = “1222-11-11”;

SimpleDateFormat formatter = new SimpleDateFormat(“yyyy-MM-dd”);

Date t = null;

try{


t = formatter.parse(input);

out.println(t);

}catch(ParseException e){


out.println(“unparseable using ” + formatter);

}

%>

输出结果为:

Fri Nov 11 00:00:00 CST 1222



7、计算日期之间的间隔

<%@ page import=”java.util.Date”%>

<%@ page import=”java.text.DateFormat”%>

<%

String input = “2003-05-01”;

SimpleDateFormat formatter = new SimpleDateFormat(“yyyy-MM-dd”);

Date d1 = null;

try{


d1 = formatter.parse(input);

}catch(ParseException e){


out.println(“unparseable using ” + formatter);

}



Date d2 = new Date();



long diff = d2.getTime() – d1.getTime();

out.println(“Difference is ” + (diff/(1000*60*60*24)) + ” days.”);

%>

输出结果为:

Difference is 29 days.



8、日期的加减运算

方法:用Calendar类的add()方法

<%@ page import=”java.util.*”%>

<%@ page import=”java.text.*”%>

<%

Calendar now = Calendar.getInstance();

SimpleDateFormat formatter = new SimpleDateFormat(“E yyyy.MM.dd ‘at’ hh:mm:ss a zzz”);

out.println(“It is now ” + formatter.format(now.getTime()));

now.add(Calendar.DAY_OF_YEAR,-(365*2));

out.println(“<br>”);

out.println(“Two years ago was ” + formatter.format(now.getTime()));

%>

输出结果为:

It is now 星期五 2003.05.30 at 01:45:32 下午 CST

Two years ago was 星期三 2001.05.30 at 01:45:32 下午 CST



9、比较日期

方法:用equals()、before()、after()方法

<%@ page import=”java.util.*”%>

<%@ page import=”java.text.*”%>

<%

DateFormat df = new SimpleDateFormat(“yyy-MM-dd”);

Date d1 = df.parse(“2000-01-01”);

Date d2 = df.parse(“1999-12-31”);



String relation = null;

if(d1.equals(d2))

relation = “the same date as”;

else if(d1.before(d2))

relation = “before”;

else

relation = “after”;

out.println(d1 +” is ” + relation + ‘ ‘ + d2);

%>

输出结果为:

Sat Jan 01 00:00:00 CST 2000 is after Fri Dec 31 00:00:00 CST 1999



10、记录一件事所花费的时间

方法:调用两次System.getTimeMillis()方法,求差值

<%@ page import=”java.text.*”%>

<%

long t0,t1;

t0 = System.currentTimeMillis();

out.println(“Cyc starts at ” + t0);

int k = 0;

for(int i =0;i<100000;i++){


k += i;

}

t1 = System.currentTimeMillis();

out.println(“<br>”);

out.println(“Cyc ends at ” + t1);

out.println(“<br>”);

out.println(“This run took ” + (t1-t0) + “ms.”);

%>



输出结果为:

Cyc starts at 1054275312432

Cyc ends at 1054275312442

This run took 10ms.



其它:如何格式化小数



<%@ page import=”java.text.*”%>

<%

DecimalFormat df = new DecimalFormat(“,###.00”);

double aNumber = 33665448856.6568975;

String result = df.format(aNumber);

out.println(result);

%>



输出结果为:

33,665,448,856.66




======================



日期比较:



在JAVA中日期的计算与比较可以使用Date和DateFormat来解决,下面是一段示例代码:



import java.text.*;

import java.util.*;



public class Test{




public static void main(String[] args){


try{


Date date=new Date();

DateFormat df=DateFormat.getDateTimeInstance();

String now=df.format(date);

System.out.println(“现在时间:”+now);



System.out.println(“现在时间是否在16:00之前:”+date.before(df.parse(“2004-12-24 16:00:00”)));

}

catch(ParseException e){System.out.print(e.getMessage());

}

}

}




*****************************************************      有是一段例子    *****************************************************



小例1:

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Date;

public class Text {

public static void main(String[] args){

SimpleDateFormat dd=new SimpleDateFormat(“yyyy-MM-dd”);

String d=dd.format(new Date());

System.out.print(d);

try {

Date date=dd.parse(“2007-12-12”);

System.out.println(date);

} catch (ParseException e) {

e.printStackTrace();

}

}

}

下面是把Int整数转换成Date

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Date;

public class Text {

public static void main(String[] args){

SimpleDateFormat dd=new SimpleDateFormat(“yyyyMMdd”);

String d=dd.format(new Date());

System.out.print(d);

int ddd=20071212;

try {

Date date=dd.parse(ddd);

System.out.println(date);

} catch (ParseException e) {

e.printStackTrace();

}

}

}

—————————————————————-

小例2:

如何获取当前时间?

String now=new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”).format(Calendar.getInstance().getTime());

Date createDate=convertDate(now); //转换成Date型

//将字符串转换成日期型的方法;

public Date convertDate(String planedDate){


Date date=new Date();

SimpleDateFormat dd=new SimpleDateFormat(“yyyy-MM-dd”);

String d=dd.format(new Date());

System.out.print(d);

try {

date=dd.parse(planedDate);

System.out.println(date);

} catch (ParseException e) {

e.printStackTrace();

return null;

}

return date;

}

—————————————————————-

小例3:

如何获得系统的year,month,day?

Calendar   c   =   Calendar.getInstance();

c.setTime(new java.util.Date());

int   year =   c.get(Calendar.YEAR);

int   month =   c.get(Calendar.MONTH)+1;

int   day   =   c.get(Calendar.DAY_OF_MONTH);

int   hour =   c.get(Calendar.HOUR_OF_DAY);

int   minute =   c.get(Calendar.MINUTE);

int   second =   c.get(Calendar.SECOND);

—————————————————————-

小例4:

JAVA中获得本地系统时间的方法

import java.util.*;



public class D

{

public static void main(String []abc)

{

int y,m,d,h,mi,s;

Calendar cal=Calendar.getInstance();

y=cal.get(Calendar.YEAR);

m=cal.get(Calendar.MONTH);

d=cal.get(Calendar.DATE);

h=cal.get(Calendar.HOUR_OF_DAY);

mi=cal.get(Calendar.MINUTE);

s=cal.get(Calendar.SECOND);

System.out.println(“现在时刻是”+y+”年”+m+”月”+d+”日”+h+”时”+mi+”分”+s+”秒”);

}

}

###########################################################

public class Main{

public static void main(String[] args)

{

java.util.Calendar c=java.util.Calendar.getInstance();

java.text.SimpleDateFormat f=new java.text.SimpleDateFormat(“yyyy年MM月dd日hh时mm分ss秒”);

System.out.println(f.format(c.getTime()));

}

}

—————————————————————-

小例5:

1>先导入两个类java.util.*; java.text.*;

2>加简单的一句话:

String date=new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”).format(Calendar.getInstance).getTime());

date就是你得到的时间,显示格式可以根据(“yyyy-MM-dd HH:mm:ss”)的格式随意调整



—————————————————————-

小例6:

在JSP页面上可以通过一下方法获取:

function printDate()

{


var today=new Date();

var day=today.getDate();

var month=today.getMonth()+1;

var year=today.getFullYear().toString();

var box1=document.stats.intoDate;

if((month/10)<1)

{


month=(“0″+month).toString();

}

if((day/10)<1)

{


day=(“0″+today.getDate()).toString();

}

box1.value=year+”-“+month+”-“+day;

}