简体   繁体   English

从CSV读取字符串并解析至今

[英]Reading string from CSV and parsing to date

I am reading from a csv file and store the string as follows: 我正在读取一个csv文件,并将字符串存储如下:

                date=st.nextToken();

The format of the date is 2014-09-17T12:06:00.0000000Z , so I define: 日期的格式为2014-09-17T12:06:00.0000000Z ,因此我定义:

 SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSSSX");

When I try to parse, 当我尝试解析时

                current_date=formatter.parse(date);

I get an exception: 我有一个例外:

java.text.ParseException: Unparseable date: "Date"

Any ideas? 有任何想法吗?

For me the following works: 对我而言,以下作品:

public static void main(String[] args) throws ParseException {
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSSSX");

    System.out.println(formatter.parse("2014-09-17T12:06:00.0000000Z"));
}

So I guess the problem is elsewhere. 所以我想问题出在其他地方。 Either you receive data that is not as you expect it to be, or there is a typo somewhere? 您收到的数据不是您期望的那样,还是某个地方出现错字?

Try to print all dates before you parse them, so you know what you actually parse. 在解析日期之前,请尝试打印所有日期,以使您知道实际解析的内容。

你可以试试这个

Date date = DatatypeFactory.newInstance().newXMLGregorianCalendar(str).toGregorianCalendar().getTime();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM