java关于日期的运算等处理方法

java关于日期的运算等处理方法

http://www.blueidea.com/bbs/newsdetail.asp?id=989464

http://www.java-cn.com/bbs-jsp/show.jsp?id=133651&forum=base

java关于日期的运算等处理方法
java关于日期的运算等处理方法

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”);
}
}

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/100599.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • pandas fillna详解

    pandas fillna详解pandas中补全nan具体的参数Series.fillna(self,value=None,method=None,axis=None,inplace=False,limit=None,downcast=None,**kwargs)[source]参数: value:scalar,dict,Series,orDataFrameValuetouset…

    2022年8月12日
    4
  • 什么是按位或,什么是按位异或,什么是按位与?「建议收藏」

    什么是按位或,什么是按位异或,什么是按位与?「建议收藏」&amp;按位与|按位或^按位异或1.按位与运算按位与运算符"&amp;"是双目运算符。其功能是参与运算的两数各对应的二进位相与。只有对应的两个二进位均为1时,结果位才为1,否则为0。参与运算的数以补码方式出现。例如:9&amp;5可写算式如下:00001001(9的二进制补码)&amp;00000101(5的二进制补码)00000001(1的二进制补码)可见9&am…

    2022年6月3日
    54
  • java web项目中hibernate导入问题解决, AbstractInterceptor

    java web项目中hibernate导入问题解决, AbstractInterceptor解决步骤如下:先说一句:修改后先保存,然后看看错误消失没?只需要导入正确的包properties-&gt;javabuildpath-&gt;Libraries-&gt;AddLibaray-&gt;Myeclipselibrary-&gt;你需要导入的包-&gt;applyimportcom.opensymphony.xwork2.Action;importcom.opensym…

    2022年5月14日
    38
  • springboot实现拦截器_springmvc自定义拦截器

    springboot实现拦截器_springmvc自定义拦截器集成拦截器登录验证为例添加拦截器public class LoginInterceptor implements HandlerInterceptor { private Logger log = LoggerFactory.getLogger(getClass()); //Controller逻辑执行之前 @Override public boolean preHandle(HttpServletRequest request, HttpServletRe

    2022年8月8日
    5
  • C语言为什么被人们称为表达式语言_c语言中’0’是什么意思

    C语言为什么被人们称为表达式语言_c语言中’0’是什么意思今天无意中敲下:#includeintmain(){printf(“~0==%d\n”,~0);}输出结果是~0==-1;为什么呢?我个人的大概理解如下(不保证对错):以下假设为32位系统;0的补码是0x00000000;~0则是:0xFFFFFFFF(~是按位取反,包括不好位,跟“取反”不是一个概念)0xFFFFFFFF的原码是0

    2022年9月20日
    0
  • 基于ArUco的视觉定位

    基于ArUco的视觉定位参考如下:博客.基于ArUco的视觉定位(1-3)https://www.freesion.com/article/4265319144/基于ArUco的视觉定位(4)https://www.pianshen.com/article/2491452618/

    2022年6月22日
    23

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号