如何将字符串(yyyy-mm-dd)转换为日期格式

发布于 2024-12-06 01:16:30 字数 92 浏览 0 评论 0原文

我找到了 (yyyy-mm-dd) 格式的日期,例如 2011-09-20。 请给我将此字符串转换为日期的解决方案。

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

I have find the date in (yyyy-mm-dd) format Like this 2011-09-20.
Please give me the solution to convert this string to Date.

I want to add this date to the calender event in blackberry.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

与君绝 2024-12-13 01:16:30

我不确定我是否完全理解你的问题。如果您想获取当前日期,请使用以下代码:

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

获取当前日期,然后使用

today.toString() 

获取日、月、年等信息。 (查看 BlackBerry API 的相关信息)

如果您尝试设置日期对象,我可能会使用日历。

下面是我的代码示例,其中我根据指定日期创建日历对象,然后将其添加到 BlackBerry 日历

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();
            }
        }

祝你好运!

I'm not sure I fully understand your question. If you are trying to get the current date, use this code:

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

to get the current date, then use

today.toString() 

to get the info such as day, month, and year. (Check out the BlackBerry API's for this)

If you are trying to set a date object, I would probably use Calendar.

Here is an example of my code where I create a Calendar object based on a specified date, then add it to the BlackBerry calendar

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();
            }
        }

Good Luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文