当前位置: 技术问答>java相关
对于您来说可能很简单的日期问题,对于我很重要!
来源: 互联网 发布时间:2015-06-15
本文导语: java的日期怎样得到形如JS里面New Date得到的日期格式: Sat Jun 1 01:32:11 UTC+0800 2002 比如我在Java里用 Date nowdate = java.util.Calendar.getInstance().getTime(); request.setAttribute("nowdate",nowdate); 在JSP页面里用JS取得: var st2 =...
java的日期怎样得到形如JS里面New Date得到的日期格式:
Sat Jun 1 01:32:11 UTC+0800 2002
比如我在Java里用
Date nowdate = java.util.Calendar.getInstance().getTime();
request.setAttribute("nowdate",nowdate);
在JSP页面里用JS取得:
var st2 = "";
var dt=new Date(st2);
alert(dt);
str2的日期格式是JAVA的日期格式:Fri May 31 11:32:11 CST 2002,
但是在JS中构造为JS的日期类型alert输出就变成了Sat Jun 1 01:32:11 UTC+0800 2002 ,所以5月31日就变成了6月1日。
怎样解决这个问题?
先谢过各位了!
Sat Jun 1 01:32:11 UTC+0800 2002
比如我在Java里用
Date nowdate = java.util.Calendar.getInstance().getTime();
request.setAttribute("nowdate",nowdate);
在JSP页面里用JS取得:
var st2 = "";
var dt=new Date(st2);
alert(dt);
str2的日期格式是JAVA的日期格式:Fri May 31 11:32:11 CST 2002,
但是在JS中构造为JS的日期类型alert输出就变成了Sat Jun 1 01:32:11 UTC+0800 2002 ,所以5月31日就变成了6月1日。
怎样解决这个问题?
先谢过各位了!
|
这里肯定有你要的日期
package ptest.pre.client;
import java.util.*;
import java.text.*;
/**
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
String str="20011230";
Date dt=sdf.parse(str,new ParsePosition(0));
Calendar rightNow = Calendar.getInstance();
rightNow.setTime(dt);
rightNow.add(Calendar.DATE,2);//你要加减的日期
Date dt1=rightNow.getTime();
String reStr=sdf.format(dt1,"",new FieldPosition(0));
System.out.println(reStr);
*
*/
public class ThreeDaysAgo
{
public static void main(String args[])
{
GregorianCalendar calendar = new GregorianCalendar();
Date date = calendar.getTime ();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println ("Today: "+df.format (date));
calendar.set (Calendar.DATE,
calendar.get (Calendar.DATE)-3);
date = calendar.getTime ();
System.out.println ("Three days ago: "+df.format (date));
String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000);
// if no ids were returned, something is wrong. get out.
if (ids.length == 0)
System.exit(0);
// begin output
System.out.println("Current Time");
// create a Pacific Standard Time time zone
SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]);
// set up rules for daylight savings time
pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
// create a GregorianCalendar with the Pacific Daylight time zone
// and the current date and time
Calendar calendar1 = new GregorianCalendar(pdt);
Date trialTime = new Date();
calendar1.setTime(trialTime);
// print out a bunch of interesting things
System.out.println("ERA: " + calendar1.get(Calendar.ERA));
System.out.println("YEAR: " + calendar1.get(Calendar.YEAR));
System.out.println("MONTH: " + calendar1.get(Calendar.MONTH));
System.out.println("WEEK_OF_YEAR: " + calendar1.get(Calendar.WEEK_OF_YEAR));
System.out.println("WEEK_OF_MONTH: " + calendar1.get(Calendar.WEEK_OF_MONTH));
System.out.println("DATE: " + calendar1.get(Calendar.DATE));
System.out.println("DAY_OF_MONTH: " + calendar1.get(Calendar.DAY_OF_MONTH));
System.out.println("DAY_OF_YEAR: " + calendar1.get(Calendar.DAY_OF_YEAR));
System.out.println("DAY_OF_WEEK: " + calendar1.get(Calendar.DAY_OF_WEEK));
System.out.println("DAY_OF_WEEK_IN_MONTH: "+ calendar1.get(Calendar.DAY_OF_WEEK_IN_MONTH));
System.out.println("AM_PM: " + calendar1.get(Calendar.AM_PM));
System.out.println("HOUR: " + calendar1.get(Calendar.HOUR));
System.out.println("HOUR_OF_DAY: " + calendar1.get(Calendar.HOUR_OF_DAY));
System.out.println("MINUTE: " + calendar1.get(Calendar.MINUTE));
System.out.println("SECOND: " + calendar1.get(Calendar.SECOND));
System.out.println("MILLISECOND: " + calendar1.get(Calendar.MILLISECOND));
System.out.println("ZONE_OFFSET: "
+ (calendar1.get(Calendar.ZONE_OFFSET)/(60*60*1000)));
System.out.println("DST_OFFSET: "
+ (calendar1.get(Calendar.DST_OFFSET)/(60*60*1000)));
System.out.println("Current Time, with hour reset to 3");
calendar1.clear(Calendar.HOUR_OF_DAY); // so doesn't override
calendar1.set(Calendar.HOUR, 3);
System.out.println("ERA: " + calendar1.get(Calendar.ERA));
System.out.println("YEAR: " + calendar1.get(Calendar.YEAR));
System.out.println("MONTH: " + calendar1.get(Calendar.MONTH));
System.out.println("WEEK_OF_YEAR: " + calendar1.get(Calendar.WEEK_OF_YEAR));
System.out.println("WEEK_OF_MONTH: " + calendar1.get(Calendar.WEEK_OF_MONTH));
System.out.println("DATE: " + calendar1.get(Calendar.DATE));
System.out.println("DAY_OF_MONTH: " + calendar1.get(Calendar.DAY_OF_MONTH));
System.out.println("DAY_OF_YEAR: " + calendar1.get(Calendar.DAY_OF_YEAR));
System.out.println("DAY_OF_WEEK: " + calendar1.get(Calendar.DAY_OF_WEEK));
System.out.println("DAY_OF_WEEK_IN_MONTH: "
+ calendar1.get(Calendar.DAY_OF_WEEK_IN_MONTH));
System.out.println("AM_PM: " + calendar1.get(Calendar.AM_PM));
System.out.println("HOUR: " + calendar1.get(Calendar.HOUR));
System.out.println("HOUR_OF_DAY: " + calendar1.get(Calendar.HOUR_OF_DAY));
System.out.println("MINUTE: " + calendar1.get(Calendar.MINUTE));
System.out.println("SECOND: " + calendar1.get(Calendar.SECOND));
System.out.println("MILLISECOND: " + calendar1.get(Calendar.MILLISECOND));
System.out.println("ZONE_OFFSET: "
+ (calendar1.get(Calendar.ZONE_OFFSET)/(60*60*1000))); // in hours
System.out.println("DST_OFFSET: "
+ (calendar1.get(Calendar.DST_OFFSET)/(60*60*1000))); // in hours
}
}
记得给我分哦 :)
package ptest.pre.client;
import java.util.*;
import java.text.*;
/**
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
String str="20011230";
Date dt=sdf.parse(str,new ParsePosition(0));
Calendar rightNow = Calendar.getInstance();
rightNow.setTime(dt);
rightNow.add(Calendar.DATE,2);//你要加减的日期
Date dt1=rightNow.getTime();
String reStr=sdf.format(dt1,"",new FieldPosition(0));
System.out.println(reStr);
*
*/
public class ThreeDaysAgo
{
public static void main(String args[])
{
GregorianCalendar calendar = new GregorianCalendar();
Date date = calendar.getTime ();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println ("Today: "+df.format (date));
calendar.set (Calendar.DATE,
calendar.get (Calendar.DATE)-3);
date = calendar.getTime ();
System.out.println ("Three days ago: "+df.format (date));
String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000);
// if no ids were returned, something is wrong. get out.
if (ids.length == 0)
System.exit(0);
// begin output
System.out.println("Current Time");
// create a Pacific Standard Time time zone
SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]);
// set up rules for daylight savings time
pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
// create a GregorianCalendar with the Pacific Daylight time zone
// and the current date and time
Calendar calendar1 = new GregorianCalendar(pdt);
Date trialTime = new Date();
calendar1.setTime(trialTime);
// print out a bunch of interesting things
System.out.println("ERA: " + calendar1.get(Calendar.ERA));
System.out.println("YEAR: " + calendar1.get(Calendar.YEAR));
System.out.println("MONTH: " + calendar1.get(Calendar.MONTH));
System.out.println("WEEK_OF_YEAR: " + calendar1.get(Calendar.WEEK_OF_YEAR));
System.out.println("WEEK_OF_MONTH: " + calendar1.get(Calendar.WEEK_OF_MONTH));
System.out.println("DATE: " + calendar1.get(Calendar.DATE));
System.out.println("DAY_OF_MONTH: " + calendar1.get(Calendar.DAY_OF_MONTH));
System.out.println("DAY_OF_YEAR: " + calendar1.get(Calendar.DAY_OF_YEAR));
System.out.println("DAY_OF_WEEK: " + calendar1.get(Calendar.DAY_OF_WEEK));
System.out.println("DAY_OF_WEEK_IN_MONTH: "+ calendar1.get(Calendar.DAY_OF_WEEK_IN_MONTH));
System.out.println("AM_PM: " + calendar1.get(Calendar.AM_PM));
System.out.println("HOUR: " + calendar1.get(Calendar.HOUR));
System.out.println("HOUR_OF_DAY: " + calendar1.get(Calendar.HOUR_OF_DAY));
System.out.println("MINUTE: " + calendar1.get(Calendar.MINUTE));
System.out.println("SECOND: " + calendar1.get(Calendar.SECOND));
System.out.println("MILLISECOND: " + calendar1.get(Calendar.MILLISECOND));
System.out.println("ZONE_OFFSET: "
+ (calendar1.get(Calendar.ZONE_OFFSET)/(60*60*1000)));
System.out.println("DST_OFFSET: "
+ (calendar1.get(Calendar.DST_OFFSET)/(60*60*1000)));
System.out.println("Current Time, with hour reset to 3");
calendar1.clear(Calendar.HOUR_OF_DAY); // so doesn't override
calendar1.set(Calendar.HOUR, 3);
System.out.println("ERA: " + calendar1.get(Calendar.ERA));
System.out.println("YEAR: " + calendar1.get(Calendar.YEAR));
System.out.println("MONTH: " + calendar1.get(Calendar.MONTH));
System.out.println("WEEK_OF_YEAR: " + calendar1.get(Calendar.WEEK_OF_YEAR));
System.out.println("WEEK_OF_MONTH: " + calendar1.get(Calendar.WEEK_OF_MONTH));
System.out.println("DATE: " + calendar1.get(Calendar.DATE));
System.out.println("DAY_OF_MONTH: " + calendar1.get(Calendar.DAY_OF_MONTH));
System.out.println("DAY_OF_YEAR: " + calendar1.get(Calendar.DAY_OF_YEAR));
System.out.println("DAY_OF_WEEK: " + calendar1.get(Calendar.DAY_OF_WEEK));
System.out.println("DAY_OF_WEEK_IN_MONTH: "
+ calendar1.get(Calendar.DAY_OF_WEEK_IN_MONTH));
System.out.println("AM_PM: " + calendar1.get(Calendar.AM_PM));
System.out.println("HOUR: " + calendar1.get(Calendar.HOUR));
System.out.println("HOUR_OF_DAY: " + calendar1.get(Calendar.HOUR_OF_DAY));
System.out.println("MINUTE: " + calendar1.get(Calendar.MINUTE));
System.out.println("SECOND: " + calendar1.get(Calendar.SECOND));
System.out.println("MILLISECOND: " + calendar1.get(Calendar.MILLISECOND));
System.out.println("ZONE_OFFSET: "
+ (calendar1.get(Calendar.ZONE_OFFSET)/(60*60*1000))); // in hours
System.out.println("DST_OFFSET: "
+ (calendar1.get(Calendar.DST_OFFSET)/(60*60*1000))); // in hours
}
}
记得给我分哦 :)
|
To format a date for the current Locale, use one of the static factory methods:
DateFormat formater = SimpleDateFormat.getDateInstance();
formater.applyPattern(String pattern);
Date date = new Date();
myString = formater.format(date);
public void applyPattern(String pattern)
Date and Time Pattern Result
"yyyy.MM.dd G 'at' HH:mm:ss z" --- 2001.07.04 AD at 12:08:56 PDT
"EEE, MMM d, ''yy" --- Wed, Jul 4, '01
"h:mm a" --- 12:08 PM
"hh 'o''clock' a, zzzz" --- 12 o'clock PM, Pacific Daylight Time
"K:mm a, z" --- 0:08 PM, PDT
"yyyyy.MMMMM.dd GGG hh:mm aaa" --- 02001.July.04 AD 12:08 PM
"EEE, d MMM yyyy HH:mm:ss Z" --- Wed, 4 Jul 2001 12:08:56 -0700
"yyMMddHHmmssZ" --- 010704120856-0700
|
如
SimpleDateFormat formatter
= new SimpleDateFormat ("yyyy.MM.dd G 'at' hh:mm:ss a zzz");
Date currentTime_1 = new Date();
String dateString = formatter.format(currentTime_1);
参数:
Symbol Meaning Presentation Example
------ ------- ------------ -------
G era designator (Text) AD
y year (Number) 1996
M month in year (Text & Number) July & 07
d day in month (Number) 10
h hour in am/pm (1~12) (Number) 12
H hour in day (0~23) (Number) 0
m minute in hour (Number) 30
s second in minute (Number) 55
S millisecond (Number) 978
E day in week (Text) Tuesday
D day in year (Number) 189
F day of week in month (Number) 2 (2nd Wed in July)
w week in year (Number) 27
W week in month (Number) 2
a am/pm marker (Text) PM
k hour in day (1~24) (Number) 24
K hour in am/pm (0~11) (Number) 0
z time zone (Text) Pacific Standard Time
' escape for text (Delimiter)
'' single quote (Literal) '
SimpleDateFormat formatter
= new SimpleDateFormat ("yyyy.MM.dd G 'at' hh:mm:ss a zzz");
Date currentTime_1 = new Date();
String dateString = formatter.format(currentTime_1);
参数:
Symbol Meaning Presentation Example
------ ------- ------------ -------
G era designator (Text) AD
y year (Number) 1996
M month in year (Text & Number) July & 07
d day in month (Number) 10
h hour in am/pm (1~12) (Number) 12
H hour in day (0~23) (Number) 0
m minute in hour (Number) 30
s second in minute (Number) 55
S millisecond (Number) 978
E day in week (Text) Tuesday
D day in year (Number) 189
F day of week in month (Number) 2 (2nd Wed in July)
w week in year (Number) 27
W week in month (Number) 2
a am/pm marker (Text) PM
k hour in day (1~24) (Number) 24
K hour in am/pm (0~11) (Number) 0
z time zone (Text) Pacific Standard Time
' escape for text (Delimiter)
'' single quote (Literal) '
|
你应该取出YYYY,MM,DD这样的数据再放到NEW DATE()中去才行啦!