Android / Java日期格式化和拆分

时间:2013-07-18 12:37:43

标签: java android date formatting split

我的日期为字符串“18.07.2013 15:31:10”,我希望它像

一样分开
string DAY_OF_WEEK_IN_MONTH = "18";
string DAY_OF_WEEK = "Thursday";
string MONTH_IN_YEAR = "July";
string HOUR ="15:31";

我该怎么做?我看过格式化帖子,但不知道如何分割日期。

2 个答案:

答案 0 :(得分:1)

SimpleDateFormat format  = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
Date date = format.parse("18.07.2013 15:31:10");
Calendar calendar = Calendar.getInstance();
calenadr.setTime(date);

然后您可以使用calendar获取所需信息。

int dayOfTheWeek = calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH);

答案 1 :(得分:0)

   SimpleDateFormat format  = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
   Date date = format.parse("18.07.2013 15:31:10");
   Calendar cal = Calendar.getInstance();
   cal.setTime(date);             
   int day  = cal.get(Calendar.DATE);
   int month  = cal.get(Calendar.MONTH) + 1;
   int year = cal.get(Calendar.YEAR);        
   System.out.println("day:" + day);
   System.out.println("month :"+month);       
   System.out.println("year: "+year);
   System.out.println(new SimpleDateFormat("MMM").format(cal.getTime()));
   System.out.println(new SimpleDateFormat("EEE").format(cal.getTime()));
   System.out.println(new SimpleDateFormat("HH:mm").format(cal.getTime()));