//字符串转化成时间类型(字符串可以是任意类型,只要和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(“”);
SimpleDateFormat formatter = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
String mDateTime1=formatter.format(d);
out.println(mDateTime1);
out.println(“”);
out.println(d.getTime());
out.println(“”);
//当前时间
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(“”);
//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(“”);
//明天日期
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(“”);
//两个时间之间的天数
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(“”);
//加半小时
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(“”);
//年月周求日期
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(“”);
//求是星期几
mydate= myFormatter.parse(“2001-1-1”);
SimpleDateFormat formatter4 = new SimpleDateFormat(“E”);
String mydate3=formatter4.format(mydate);
out.println(mydate3);
out.println(“”);
%>
jsp显示当前时间及日期
import java.util.*;
public class Mydate
{
public static void main(String[] args)
{
Calendar cal = Calendar.getInstance();
String date_now=cal.get(cal.YEAR)+”年”+(cal.get(cal.MONTH)+1)+”月”+cal.get(cal.DATE)+”日 “;
String time_now=cal.get(cal.HOUR_OF_DAY)+”时”+cal.get(cal.MINUTE)+”分”+cal.get(cal.SECOND)+”秒”;
System.out.println(date_now);
System.out.println(time_now);
}
}
import java.util.Date;
public class test {
public static void main(String[] args){
Date date = new Date();
int year=date.getYear()+1900;
int month=date.getMonth()+1;
int day=date.getDate();
System.out.println(“It is “+year+”/”+month+”/”+day+” (YYYY/MM/DD)today.”);
}
}
<%@ page import=”java.util.*, java.text.*” %>
<html>
<head>
<title>jSP to display the current time</title>
</head>
<body>
The current time is:
<%
Date now = new Date();
out.println(DateFormat.getTimeInstance().format(now));
%>
</body>
</html>
/**
* 获取当前时间,返回时间格式(如果调用参数为true时返回yyyy-MM-dd HH:mm:ss
* ,否则为false时返回yyyy-MM-DD不带日期格式)
* @param time boolean
* @return String
* @throws Exception
*/
public static String getNowTime(boolean time) throws java.lang.Exception {
Date now = new Date();
String format = “”;
//yyyy-MM-dd HH:mm:ss:S 年月日时分秒毫杪
if (time) {
format = “yyyy-MM-dd HH:mm:ss”;
} else {
format = “yyyy-MM-dd”;
}
SimpleDateFormat sdf = new SimpleDateFormat(format);
String nowtime = sdf.format(now);
return nowtime;
}