欢迎光临
我们一直在努力

java关于日期的运算等处理方法-JSP教程,Java技巧及代码

建站超值云服务器,限时71元/月

jsp中的日期问题及其它:d :d :d

/**

* @author imagebear

*/

日期问题

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

在网上经常看到有人问如何将 获得当前时间并转换成

yyyy-mm-dd 年-月-日

hh:mm:ss 小时-分钟-秒

yyyy-mm-dd hh:mm:ss 年-月-日 小时-分钟-秒

三种格式 下面就是 jsp gui 的使用方法

<!–

jsp获得当前日期

–>

<!– 导入处理时间类,此类内部都是静态方法,直接调用即可. –>

<%@ page import="com.mamak.util.timestring" %>

<%

//获得当前日期时间

string nowdate = timestring.getnowtime("yyyy-mm-dd");

string nowtime = timestring.getnowtime("hh:mm:ss");

string nowdatetime = timestring.getnowtime("yyyy-mm-dd hh:mm:ss");

out.println("nowdate: "+nowdate);

out.println("nowtime: "+nowtime);

out.println("nowdatetime: "+nowdatetime);

%>

//******************************************************

//gui 或java 小程序获得得当前日期

public class test()

{

public static void main(string abc[])

{

//直接包名点类名点方法名使用

system.out.println("nowdate: "+com.mamak.util.timestring.getnowtime("yyyy-mm-dd"));

system.out.println("nowtime: "+com.mamak.util.timestring.getnowtime("hh:mm:ss"));

system.out.println("nowdatetime: "+com.mamak.util.timestring.getnowtime("yyyy-mm-dd hh:mm:ss"));

}

}

//******************************************************

//获得时间的bean 文件名 timestring.java

package com.mamak.util;

import java.text.simpledateformat;

import java.util.calendar;

public class timestring

{

public timestring()

{

}

public static string getnowtime(string timeformat)

{

simpledateformat lformat = new simpledateformat(timeformat);

calendar now = calendar.getinstance();

string nowstr = lformat.format(now.gettime());

return nowstr;

}

public static string getnottime()

{

return getnowtime("yyyy-mm-dd");

}

}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » java关于日期的运算等处理方法-JSP教程,Java技巧及代码
分享到: 更多 (0)

相关推荐

  • 暂无文章