从Android中的给定字符串获取日期

时间:2012-07-17 09:43:49

标签: android datetime date

您好我是Android新手。任何人都可以帮助我从2012-07-15获取15 July之类的日期。我知道这是通过使用DateFormater完成的,但我不知道它是如何工作的?请帮我找到这个。

3 个答案:

答案 0 :(得分:1)

试试这个。

public static String convertStringToDate(String startDate) throws ParseException
    {
        String myFormatString = "dd-MMM-yyyy"; // Your startDate Format
        SimpleDateFormat df = new SimpleDateFormat(myFormatString);
        Date startingDate = df.parse(startDate);

        DateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd");  // Date format what you want
        return dateFormat.format(startingDate);
    }

答案 1 :(得分:1)

ii通过使用subString()方法

来使用它
   String TimeZOne = "2012-07-08" 
    String Year = TimeZOne.substring(0,4); 
    String month = TimeZOne.substring(4,7); 
    String date = TimeZOne.substring(8,10);

    int Month_1 = Integer.parseInt(month);
    String month_String = find_Month(date_1);//call method

    String date_Fromate = date+" "+month_String ; 

//声明方法

public String Date_String(int i){

switch (i) 
                {
                    case (i==1): 
                    {
                        month = "JAN";
                        break;
                    }
                    case (i==2): 
                    {
                        month = "FEB";
                        break;
                    }
                    case (i==3): 
                    {
                        month = "MAR";
                        break;
                    }
                    case (i==4): 
                    {
                        month = "APR";
                        break;
                    }
                    case (i==5): 
                    {
                        month = "MAY";
                        break;
                    }
                    case (i==6): 
                    {
                        month = "JUN";
                        break;
                    }
                    case (i==7): 
                    {
                        month = "JUL";
                        break;
                    }
                    case (i==8): 
                    {
                        month = "AUG";
                        break;
                    }
                    case (i==9): 
                    {
                        month = "SEP";
                        break;
                    }
                    case (i==10): 
                    {
                        month = "OCT";
                        break;
                    }
                    case (i==11): 
                    {
                        month = "NOV";
                        break;
                    }
                    case (i==12): 
                    {
                        month = "DEC";
                        break;
                    }
                }
         return month;
}

答案 2 :(得分:0)

尝试此代码示例

    String date1 = "15 July 2012";
    SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy");
    Date testDate = null;
    try {
        testDate = sdf.parse(date1);
    }catch(Exception ex){
        ex.printStackTrace();
    }
    System.out.println(testDate);

    sdf = new SimpleDateFormat("yyyy-MM-dd");
    date1 = sdf.format(testDate); 
    System.out.println(date1);