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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • matlab怎么定义矩阵函数,MATLAB数组的常用函数

    3进行数组运算的常用函数在MATLAB中有一些常用函数,这些函数在日常的编程计算过程中会经常遇到,一般是基本的数学概念在MATLAB中的函数表达方式。这些函数在MATLAB中可以同时作用于整个矩阵或者数组,应用起来非常方便,不需要再另写循环程序来对各元素分别进行计算。掌握这些函数是进一步学习的基础。MATLAB人性化的地方在于其自带函数基本是按照相对应的英文名称缩写而来,所以便于记忆。3.1…

    2022年4月8日
  • 将pip源更换到国内镜像「建议收藏」

    将pip源更换到国内镜像「建议收藏」将pip源更换到国内镜像用pip管理工具安装库文件时,默认使用国外的源文件,因此在国内的下载速度会比较慢,可能只有50KB/s。幸好,国内的一些顶级科研机构已经给我们准备好了各种镜像,下载速度可达2MB/s。其中,比较常用的国内镜像包括:(1)阿里云http://mirrors.aliyun.com/pypi/simple/(2)豆瓣http://pypi.douban.com/simp…

    2022年5月4日
  • 妈妈再也不用担心我的博客访问量了(一个可以刷博客访问量的小程序java)「建议收藏」

    妈妈再也不用担心我的博客访问量了(一个可以刷博客访问量的小程序java)「建议收藏」前言:相信很多写博客的朋友,会苦恼于博客访问量上不去的问题。博主最近工作比较新手动谢了个小程序,一个可以刷访问量的程序。当然主要是针对csdn博客。有了他就再也不必担心博客访问量上不去的问题了。注:博客最主要还是要自己做好seo优化,以及提高博客内容质量。本篇文章,仅供学习交流。大家如果有问题,可以留言。一,废话少说,看图:软件点击jar包直接运行:如下:稍后查看博客访问量会有变化哟,博主

    2022年6月17日
  • Android开发CompoundButton抽象类控件类的使用UI之Radio、Check、Toggle[通俗易懂]

    Android开发CompoundButton抽象类控件类的使用UI之Radio、Check、Toggle[通俗易懂]本篇文章就讲解了一下CompoundButton抽象类下的三个实现控件类的使用,在Android4.0之后,又新加入了一个控Switch,对它的使用与之上介绍的三个控件类似,这里就不再详细讲解了。前言这篇文章讲解一下Android平台下,RadioButton、CheckBox以及ToggleButton三个控件的用法,因为这三个控件之中都存在一个选中或是没选中的状态,所以

    2022年5月2日
  • 三立[通俗易懂]

    三立[通俗易懂]自传173、在佳木斯上网写作2013年1—-互动百科二零一三年一月起,我在佳木斯市的上网写作进入了第七年时间,钰香妻在市健身房练打乒乓球,照看外孙女和管理家务吃喝用穿。大女儿女婿在厦门外国语学校,二女儿女婿在本市文化公司上班。儿子在大连工业大学,儿媳在大连银行,孙女在北京上大学二年。2013、1、1星期二写《新年(七古)》:平平静静日历翻,新页…

    2022年5月4日
  • 在线数据库设计工具-toolfk程序员在线工具网

    在线数据库设计工具-toolfk程序员在线工具网本文要推荐的[ToolFk]是一款程序员经常使用的线上免费测试工具箱,ToolFk特色是专注于程序员日常的开发工具,不用安装任何软件,只要把内容贴上按一个执行按钮,就能获取到想要的内容结果。ToolFk还支持BarCode条形码在线生成、QueryList采集器、PHP代码在线运行、PHP混淆、加密、解密、Python代码在线运行、JavaScript在线运行、YAML格式化…

    2022年7月11日

发表回复

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

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