JAVA中的时间操作

来源:互联网 发布:数控车床编程培训 编辑:程序博客网 时间:2024/06/05 00:54
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
 

JAVA中的时间操作

经常看见jsp版里有人问时间操作的问题,这些问题一般包括:取当前时间,把一个指定的字符串时间转化成时间类型,求两个时间之间的天数,求一段时间以前的时间,求一段时间以后的时间,在这里就把这些问题汇总一下。

<%@  page  contentType="text/html;charset=gb2312"%>

<%@ page import="JAVA.text.*"%>

<%@ page import="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");

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss G E D F w W a E F");

   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>");

%>

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>