BlackBerry:检索当前年,月,日

时间:2012-01-07 02:30:37

标签: java blackberry

//get current date
SimpleDateFormat monthFormat = new SimpleDateFormat("M");
int _currentMonth = Integer.parseInt(monthFormat.format(new Date(System.currentTimeMillis())));

SimpleDateFormat yearFormat = new SimpleDateFormat("YYYY");
int _currentYear = Integer.parseInt(yearFormat.format(new Date(System.currentTimeMillis())));

我正在尝试构建一个日历视图,而我首先要检索当前的年份和月份。但是,返回的是一个字符串,而我想在数组等中使用int ..

上面发布的代码很可能是由于Integer.parseInt函数导致的错误。

我可以不使用它吗?检索年,月和日的最佳方法是什么。

3 个答案:

答案 0 :(得分:1)

您可以使用日历类来获取时间

默认日期是指当前日期:

Calendar cal=Calendar.getInstance();
        System.out.println("YEAR:"+cal.get(Calendar.YEAR)+" MONTH: "+cal.get(Calendar.MONTH)+1+"  DAY: "+cal.get(Calendar.DAY_OF_MONTH));
        //out put as YEAR:2012 MONTH: 01  DAY: 7

这是指定的日期:

Date date1=new Date(Long.parseLong("13259152444455"));//or Date date1=new Date(13259152444455L);
            cal=Calendar.getInstance();
            cal.setTime(date1);
            System.out.println("YEAR:"+cal.get(Calendar.YEAR)+" MONTH: "+cal.get(Calendar.MONTH)+1+"  DAY: "+cal.get(Calendar.DAY_OF_MONTH));
    //      output as YEAR:2390 MONTH: 21  DAY: 2

答案 1 :(得分:0)

是的,它确实有效!我必须使用yyyy而不是YYYY。黑莓文档将其视为YYYY

答案 2 :(得分:0)

SimpleDateFormat monthFormat = new SimpleDateFormat("MM");
int _currentMonth = Integer.parseInt(monthFormat.format(new Date(System.currentTimeMillis())));

SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy");
int _currentYear = Integer.parseInt(yearFormat.format(new Date(System.currentTimeMillis())));

将“M”改为“MM”,将“YYYY”改为“yyyy”。

相关问题