我思故我强

java中的日期和时间处理示例

 

//日期和时间处理示例

import java.util.*;
import java.text.SimpleDateFormat;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;

public class TestDateTime
{
public static void main(String[] argv)
{
long nCurrentTime = System.currentTimeMillis();
java.util.Date utilDate = new java.util.Date(nCurrentTime);

//以下是用于数据库操作的日期和时间类
java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime()); //java.util.Date和java.sql.Date的转换
java.sql.Time sqlTime = new java.sql.Time(utilDate.getTime());
java.sql.Timestamp sqlTimestamp = new java.sql.Timestamp(nCurrentTime);

//Calendar用于获取指定的时间项(字段)
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(nCurrentTime);

int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1; //注意:返回的月份是基于0的!
int day = calendar.get(Calendar.DAY_OF_MONTH);
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);

System.out.println("Current date is: " + year + "年" + month + "月" + day + "日");
System.out.println("Current time is: " + hour + "时" + minute + "分" + second + "秒");

//SimpleDateFormat用于格式化日期和时间
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String szDatetime1 = df.format(utilDate);
System.out.println("Current datetime is: " + szDatetime1);

//以下计算两个日期值之间相差的时间
java.util.Date date2 = new java.util.Date(nCurrentTime - 24600 * 1000);

long nMilliSeconds = utilDate.getTime() - date2.getTime();
long nSeconds = nMilliSeconds / 1000; //把毫秒换算成秒
double nDays = nSeconds / (24d * 60d * 60d); //把秒换算成天

String szDatetime2 = df.format(date2);
System.out.println("Time between " + szDatetime1 + " and " + szDatetime2 + " is " + nSeconds + " seconds (" + nDays + " days)");
}
}

-----------------------------------
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.text.*,java.util.*"%>

<%
// 字符串转化成时间类型(字符串可以是任意类型,只要和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");
out.println(d);
out.println("<br>");
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String mDateTime1=formatter.format(d);
out.println(mDateTime1);
out.println("<br>");
out.println(d.getTime());
out.println("<br>");
//当前时间
Calendar cal = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String mDateTime=formatter.format(cal.getTime());
out.println(mDateTime);
out.println("<br>");

//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);
out.println(mDate);
out.println("<br>");

//明天日期
myDate=new java.util.Date();
myTime=(myDate.getTime()/1000)+60*60*24;
myDate.setTime(myTime*1000);
mDate=formatter.format(myDate);
out.println(mDate);
out.println("<br>");

//两个时间之间的天数
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);
out.println(day);
out.println("<br>");

//加半小时
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);
out.println(mydate1);
out.println("<br>");

//年月周求日期
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);
out.println(mydate2);
out.println("<br>");

//求是星期几
mydate= myFormatter.parse("2001-1-1");
SimpleDateFormat formatter4 = new SimpleDateFormat("E");
String mydate3=formatter4.format(mydate);
out.println(mydate3);
out.println("<br>");
%>

posted on 2008-07-15 15:52 李云泽 阅读(255) 评论(0)  编辑  收藏 所属分类: Java代码


只有注册用户登录后才能发表评论。


网站导航: