转换表格2012,12,06,18,00,00上的字符串以创建新的格里高利历

时间:2012-12-06 23:42:53

标签: java android xml xml-parsing

您好我有以下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>

我可以阅读每个变量并将它们放在一个arraylist中,所有 EXCEPT &#34; timeUpdated&#34;我无法设法从表单上的字符串转换的变量&#34; 2012,12,06,18,00,00&#34;投入如下的新格里高利历

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

所以我的问题如何在表单上转换字符串&#34; 2012,12,06,16,30,55&#34;能够投入

GregorianCalendar已更新=新GregorianCalendar(&#34;我转换后的日历字符串&#34;);

如果你想知道这是我的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");
    }
}

1 个答案:

答案 0 :(得分: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);