我的String和Date之间的转换有问题,那位给我看看啊

iceandfire 2002-11-17 05:12:38
import java.util.*;
import java.text.SimpleDateFormat;

class Queue3 extends Vector
{
public static void main(String[] args)
{
String[] s={"20020809","20020810","20020811"};
Queue3 queue=new Queue3(s);
String[] s1=new String[5];
Date[] dt=new Date[5];

for (int i=0;i<s.length;i++)
{
s1[i]=(String)queue.dequeue();
SimpleDateFormat df=new SimpleDateFormat("yyyymmdd");
dt[i]=df.parse(s1[i]);
}
for (int i=0;i<s.length;i++)
{
System.out.println(" dt["+i+"]="+dt[i]);
}
}

Queue3()
{
super();
}
Queue3(int capacity)
{
super(capacity);
System.out.println("call this()");
}
Queue3(String[] objs)
{
this(objs.length);
for (int index=0;index<objs.length;index++)
{
addElement(objs[index]);
}
}

Object dequeue()
{
Object obj=firstElement();
boolean success=removeElement(obj);
return obj;
}
}

出错提示:
Queue3.java:17: unreported exception java.text.ParseException; must be caught or
declared to be thrown
dt[i]=df.parse(s1[i]);
^
多谢了~~
...全文
55 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
dt[i]=df.parse(s1[i])-->dt[i]=df.parse(s1[i],new ParsePosition(0));


Iforgot 2002-11-17
  • 打赏
  • 举报
回复
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

是JavaDoc说明。
Iforgot 2002-11-17
  • 打赏
  • 举报
回复
用“yyyyMMdd”大写M、小写的表示 minute做分钟解析了。
aasy 2002-11-17
  • 打赏
  • 举报
回复
他的月份本来就不符合我们的标准,你的更奇怪而已。
iceandfire 2002-11-17
  • 打赏
  • 举报
回复
我捕获的是catch(ParseException e)
可结果不是我想象的那样啊
call this()

dt[0]=Wed Jan 09 00:08:00 CST 2002

dt[1]=Thu Jan 10 00:08:00 CST 2002

dt[2]=Fri Jan 11 00:08:00 CST 2002

原来的三个字符串20020809 20020810 20020811
怎么转换后不是8月了?如果我只想输出年月日,我该如何做呢?
dentance 2002-11-17
  • 打赏
  • 举报
回复
import java.util.*;
import java.text.SimpleDateFormat;

class Queue3 extends Vector
{
public static void main(String[] args)
{
String[] s={"20020809","20020810","20020811"};
Queue3 queue=new Queue3(s);
String[] s1=new String[5];
Date[] dt=new Date[5];

for (int i=0;i<s.length;i++)
{
s1[i]=(String)queue.dequeue();
SimpleDateFormat df=new SimpleDateFormat("yyyymmdd");
try{
dt[i]=df.parse(s1[i]);
}
catch(Exception e){}
}
for (int i=0;i<s.length;i++)
{
System.out.println(" dt["+i+"]="+dt[i]);
}
}

Queue3()
{
super();
}
Queue3(int capacity)
{
super(capacity);
System.out.println("call this()");
}
Queue3(String[] objs)
{
this(objs.length);
for (int index=0;index<objs.length;index++)
{
addElement(objs[index]);
}
}

Object dequeue()
{
Object obj=firstElement();
boolean success=removeElement(obj);
return obj;
}
}

iceandfire 2002-11-17
  • 打赏
  • 举报
回复
如果不Try catch这段代码就是有问题的吗?
netwebs 2002-11-17
  • 打赏
  • 举报
回复
呵呵~~~~提示你要捕获异常啊
怎么能不try catch呢

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧