java中,怎样把 20 Dec 2012 10am 这样的时间字符串转换成Date类型的??要求的格式是1970-12-12 14:07:00
展开全部
package baidu;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
public class DateTest1 {
public static int trans(String str){
int result=0;
if ("Dec".equals(str)){
result=11;
}//这个地方需要12个月份的转化。
return result;
}
public static void main(String[] args) {
String str="20 Dec 2012 10am 11 30";
String[] arr=str.split(" ");
int year =Integer.parseInt(arr[2]);
int month =trans(arr[2]);
int date =Integer.parseInt(arr[0]);
int hour =Integer.parseInt(arr[3].replace("am", "").replace("pm", ""));
int minute =Integer.parseInt(arr[4]);
int second =Integer.parseInt(arr[5]);
GregorianCalendar cal=new GregorianCalendar();
cal.set(year, month, date, hour, minute, second);
System.out.println( new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(cal.getTime()));
}
}
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
public class DateTest1 {
public static int trans(String str){
int result=0;
if ("Dec".equals(str)){
result=11;
}//这个地方需要12个月份的转化。
return result;
}
public static void main(String[] args) {
String str="20 Dec 2012 10am 11 30";
String[] arr=str.split(" ");
int year =Integer.parseInt(arr[2]);
int month =trans(arr[2]);
int date =Integer.parseInt(arr[0]);
int hour =Integer.parseInt(arr[3].replace("am", "").replace("pm", ""));
int minute =Integer.parseInt(arr[4]);
int second =Integer.parseInt(arr[5]);
GregorianCalendar cal=new GregorianCalendar();
cal.set(year, month, date, hour, minute, second);
System.out.println( new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(cal.getTime()));
}
}
展开全部
字符串形式有点像new java.util.Date().toGMTString()得来的,不过和你写的还不太一样。
如果这“21 Dec 2012 07:51:37 GMT”这种的话Date的过期方法倒是可以用。
java.util.Date date = new java.util.Date(java.util.Date.parse("21 Dec 2012 07:51:37 GMT"));
System.out.println(date.toString());
如果这“21 Dec 2012 07:51:37 GMT”这种的话Date的过期方法倒是可以用。
java.util.Date date = new java.util.Date(java.util.Date.parse("21 Dec 2012 07:51:37 GMT"));
System.out.println(date.toString());
追问
那个好像已过时了,有没有其他方法啊!??
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
System.Globalization.DateTimeFormatInfo myDTFI = new System.Globalization.CultureInfo( "en-US", false ).DateTimeFormat;
Response.Write(DateTime.Now.ToString("F", myDTFI)+"<br/>");
string str="Mon, 28 Nov 2005 17:55:58 CST";
str=str.Replace("CST","GMT");//因为CST=GMT+8.所以可以先把它转换成GMT的,然后加8小时就可以了
Response.Write(Convert.ToDateTime(str).AddHours(8).ToString("f",myDTFI));
/*
This code produces the following output.
FORMAT en-US EXAMPLE
CHAR VALUE OF ASSOCIATED PROPERTY, IF ANY
d 1/3/2002
M/d/yyyy (ShortDatePattern)
D Thursday, January 03, 2002
dddd, MMMM dd, yyyy (LongDatePattern)
f Thursday, January 03, 2002 12:00 AM
F Thursday, January 03, 2002 12:00:00 AM
dddd, MMMM dd, yyyy h:mm:ss tt (FullDateTimePattern)
g 1/3/2002 12:00 AM
G 1/3/2002 12:00:00 AM
m January 03
MMMM dd (MonthDayPattern)
M January 03
MMMM dd (MonthDayPattern)
r Thu, 03 Jan 2002 00:00:00 GMT
ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (RFC1123Pattern)
R Thu, 03 Jan 2002 00:00:00 GMT
ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (RFC1123Pattern)
s 2002-01-03T00:00:00
yyyy'-'MM'-'dd'T'HH':'mm':'ss (SortableDateTimePattern)
t 12:00 AM
h:mm tt (ShortTimePattern)
T 12:00:00 AM
h:mm:ss tt (LongTimePattern)
u 2002-01-03 00:00:00Z
yyyy'-'MM'-'dd HH':'mm':'ss'Z' (UniversalSortableDateTimePattern)
U Thursday, January 03, 2002 8:00:00 AM
y January, 2002
MMMM, yyyy (YearMonthPattern)
Y January, 2002
MMMM, yyyy (YearMonthPattern)
*/
Response.Write(DateTime.Now.ToString("F", myDTFI)+"<br/>");
string str="Mon, 28 Nov 2005 17:55:58 CST";
str=str.Replace("CST","GMT");//因为CST=GMT+8.所以可以先把它转换成GMT的,然后加8小时就可以了
Response.Write(Convert.ToDateTime(str).AddHours(8).ToString("f",myDTFI));
/*
This code produces the following output.
FORMAT en-US EXAMPLE
CHAR VALUE OF ASSOCIATED PROPERTY, IF ANY
d 1/3/2002
M/d/yyyy (ShortDatePattern)
D Thursday, January 03, 2002
dddd, MMMM dd, yyyy (LongDatePattern)
f Thursday, January 03, 2002 12:00 AM
F Thursday, January 03, 2002 12:00:00 AM
dddd, MMMM dd, yyyy h:mm:ss tt (FullDateTimePattern)
g 1/3/2002 12:00 AM
G 1/3/2002 12:00:00 AM
m January 03
MMMM dd (MonthDayPattern)
M January 03
MMMM dd (MonthDayPattern)
r Thu, 03 Jan 2002 00:00:00 GMT
ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (RFC1123Pattern)
R Thu, 03 Jan 2002 00:00:00 GMT
ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (RFC1123Pattern)
s 2002-01-03T00:00:00
yyyy'-'MM'-'dd'T'HH':'mm':'ss (SortableDateTimePattern)
t 12:00 AM
h:mm tt (ShortTimePattern)
T 12:00:00 AM
h:mm:ss tt (LongTimePattern)
u 2002-01-03 00:00:00Z
yyyy'-'MM'-'dd HH':'mm':'ss'Z' (UniversalSortableDateTimePattern)
U Thursday, January 03, 2002 8:00:00 AM
y January, 2002
MMMM, yyyy (YearMonthPattern)
Y January, 2002
MMMM, yyyy (YearMonthPattern)
*/
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Date date = new Date();//这里date的值就是你这个值20 Dec 2012 10am哈,你改下就行了
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ");
String dates = sdf.format(date);
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ");
String dates = sdf.format(date);
追问
不是,是把String转换为Date。
追答
Date date = new Date();
String dates = String.valueOf(date);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date dates1 = sdf.parse(String.valueOf(date));
System.out.println(dates1);
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
java.text.SimpleDateFormat
Letter Date or Time Component Presentation Examples
G Era designator Text AD
y Year Year 1996; 96
M Month in year Month July; Jul; 07
w Week in year Number 27
W Week in month Number 2
D Day in year Number 189
d Day in month Number 10
F Day of week in month Number 2
E Day in week Text Tuesday; Tue
a Am/pm marker Text PM
H Hour in day (0-23) Number 0
k Hour in day (1-24) Number 24
K Hour in am/pm (0-11) Number 0
h Hour in am/pm (1-12) Number 12
m Minute in hour Number 30
s Second in minute Number 55
S Millisecond Number 978
z Time zone General time zone Pacific Standard Time; PST; GMT-08:00
Z Time zone RFC 822 time zone -0800
Letter Date or Time Component Presentation Examples
G Era designator Text AD
y Year Year 1996; 96
M Month in year Month July; Jul; 07
w Week in year Number 27
W Week in month Number 2
D Day in year Number 189
d Day in month Number 10
F Day of week in month Number 2
E Day in week Text Tuesday; Tue
a Am/pm marker Text PM
H Hour in day (0-23) Number 0
k Hour in day (1-24) Number 24
K Hour in am/pm (0-11) Number 0
h Hour in am/pm (1-12) Number 12
m Minute in hour Number 30
s Second in minute Number 55
S Millisecond Number 978
z Time zone General time zone Pacific Standard Time; PST; GMT-08:00
Z Time zone RFC 822 time zone -0800
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |