2011-09-20 105 views
0

我已经找到(yyyy-mm-dd)格式的日期像这样2011-09-20。 请给我解决方案将此字符串转换为日期。如何将字符串(yyyy-mm-dd)转换为日期格式

我想将此日期添加到黑莓中的日历事件。

+0

你是怎么找到String格式的日期的?你从哪里得到它? –

回答

0

我不知道我完全理解你的问题。如果您想获得当前日期,使用此代码:

java.util.Date today = new java.util.Date(); 

获得当前的日期,然后使用

today.toString() 

获得信息,如日,月,年。 (查看这个BlackBerry API)

如果你想设置一个日期对象,我可能会使用日历。

这里就是我创建一个基于指定日期的日历对象我的代码示例,然后将其添加到黑莓日历

PIM pim = PIM.getInstance(); 
      try { 

       String _date = date.getText(); 
       int _month = Integer.parseInt(_date.substring(0, _date.indexOf('/'))); 
       _date = _date.substring(_date.indexOf('/') + 1); 
       int _day = Integer.parseInt(_date.substring(0, _date.indexOf('/'))); 
       _date = _date.substring(_date.indexOf('/') + 1); 
       int _year = Integer.parseInt(_date.substring(0, _date.indexOf('/'))); 
       _year = _year + 2000; 

       EventList events = (EventList) pim.openPIMList(PIM.EVENT_LIST, PIM.READ_WRITE); 
       Event event = events.createEvent(); 
       event.addString(Event.SUMMARY, PIMItem.ATTR_NONE, title.getText()); 
       Calendar cal = Calendar.getInstance(); 
       cal.set(Calendar.YEAR, _year); 

       //this of course seems like a terrible way to set the months, but the BlackBerry 
       //api wants the month in this format 
       if(_month == 1) 
        cal.set(Calendar.MONTH, Calendar.JANUARY); 
       if(_month == 2) 
        cal.set(Calendar.MONTH, Calendar.FEBRUARY); 
       if(_month == 3) 
        cal.set(Calendar.MONTH, Calendar.MARCH); 
       if(_month == 4) 
        cal.set(Calendar.MONTH, Calendar.APRIL); 
       if(_month == 5) 
        cal.set(Calendar.MONTH, Calendar.MAY); 
       if(_month == 6) 
        cal.set(Calendar.MONTH, Calendar.JUNE); 
       if(_month == 7) 
        cal.set(Calendar.MONTH, Calendar.JULY); 
       if(_month == 8) 
        cal.set(Calendar.MONTH, Calendar.AUGUST); 
       if(_month == 9) 
        cal.set(Calendar.MONTH, Calendar.SEPTEMBER); 
       if(_month == 10) 
        cal.set(Calendar.MONTH, Calendar.OCTOBER); 
       if(_month == 11) 
        cal.set(Calendar.MONTH, Calendar.NOVEMBER); 
       if(_month == 12) 
        cal.set(Calendar.MONTH, Calendar.DECEMBER); 


       cal.set(Calendar.DATE, _day); 
       cal.set(Calendar.HOUR_OF_DAY, 17); 
       cal.set(Calendar.MINUTE, 15); 

       event.addDate(Event.START, PIMItem.ATTR_NONE, cal.getTime().getTime()); 
       cal.set(Calendar.HOUR_OF_DAY, 21); 

       event.addDate(Event.END, PIMItem.ATTR_NONE, cal.getTime().getTime()); 
       event.addString(BlackBerryEvent.LOCATION, PIMItem.ATTR_NONE, address.getText()); 
       event.addString(Event.NOTE, PIMItem.ATTR_NONE, description.getText()); 

       event.commit(); 

       Dialog.alert(title.getText() + " was added to your calendar."); 

      } catch (PIMException e) { 
       // TODO Auto-generated catch block 
       System.out.println(e.getMessage()); 
       e.printStackTrace(); 
      } 
     } 

祝您好运!

相关问题