2

您好我有以下 xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<share>
    <item>
        <name>Ericsson B</name>
        <currentRate>80.69</currentRate>
        <changeToday>-0.51</changeToday>
        <changeTodayPercent>-0.52</changeTodayPercent>
            <timeUpdated>2012,12,06,18,00,00</timeUpdated>
     </item>
    </share>

我可以读取每个变量并将它们放入一个数组列表中,除了“timeUpdated”变量,我无法将“2012,12,06,18,00,00”形式的字符串转换为如下所示的新公历

 GregorianCalendar updated = new GregorianCalendar(2012,12,06,16,30,55); 

所以我的问题是如何将“2012,12,06,16,30,55”形式的字符串转换为能够放入

GregorianCalendar updated = new GregorianCalendar("我转换的日历字符串");

如果你想知道这里是我的 xmlparser

public AllSharesOnStockMarket(){

    XMLParser parser = new XMLParser();
    String xml = parser.getXmlFromUrl(URL); // getting XML
    Document doc = parser.getDomElement(xml); // getting DOM element
    NodeList nl = doc.getElementsByTagName(KEY_ITEM);
    for (int i = 0; i < nl.getLength(); i++) {
        Element e = (Element) nl.item(i);
        System.out.println(e);
        System.out.println("hejsan");
        System.out.println(parser.getValue(e, KEY_NAME));


        allShareHoldingsOnStockMarket.get(i).setName(parser.getValue(e, KEY_NAME));
        tempDouble = Double.parseDouble(parser.getValue(e, KEY_CURRENTRATE));
        allShareHoldingsOnStockMarket.get(i).setCurrentRate(tempDouble);
        tempDouble = Double.parseDouble(parser.getValue(e, KEY_CHANGETODAY));
        allShareHoldingsOnStockMarket.get(i).setCurrentRate(tempDouble);
        tempDouble = Double.parseDouble(parser.getValue(e, KEY_CHANGETODAYPERCENT));
        allShareHoldingsOnStockMarket.get(i).setChangeTodayPercent(tempDouble);

        String dateToConvert = parser.getValue(e, KEY_TIMEUPDATED);//Need to convert this into a date so I can put into 
        allShareHoldingsOnStockMarket.get(i).setTimeUpdated("My Converted Date String That is the converted dateToConvert String");
    }
}
4

1 回答 1

3

试试看

    Date date = new SimpleDateFormat("yyyy,MM,dd,HH,mm,ss").parse("2012,12,06,18,00,00");
    GregorianCalendar gc = new GregorianCalendar();
    gc.setTime(date);
于 2012-12-06T23:49:10.217 回答