Adam
Adam

Reputation: 9049

BlackBerry: Retrieving the current year, month, day

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

I'm trying to build a calendar view and I'm starting by retrieving the currrent year and month. However, what is returned is a string, whereas I want a int to use in arrays etc..

The code posted above is giving me an error most likely due to the Integer.parseInt function.

Can I not use it? What is the best way of retrieving the year, month, and day.

Upvotes: 1

Views: 623

Answers (3)

Govindarao Kondala
Govindarao Kondala

Reputation: 2862

You can use calander class to get time

This is for default date means current date :

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

This is specified date :

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

Upvotes: 1

V.J.
V.J.

Reputation: 9580

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

Change "M" to "MM" and "YYYY" to "yyyy".

Upvotes: 0

Adam
Adam

Reputation: 9049

duh it does work! I have to use yyyy instead of YYYY. The blackberry documentation had it as YYYY

Upvotes: 0

Related Questions