ubundude
ubundude

Reputation: 89

Set date of calendar

Ok, so what I'm trying to do is to set the set the date of a calendar instance, and then return the week_of_year. I am doing so by using the Calendar.set() functio

public String weekInYearForm = "ww";
SimpleDateFormat formWIM = new SimpleDateFormat(weekInYearForm, Locale.US);

Calendar lc = Calendar.getInstance();
    lc.set(Calendar.YEAR, lYear);
    lc.set(Calendar.MONTH, lMonth);
    lc.set(Calendar.DAY_OF_MONTH, lDay);


    wiy = formWIM.format(lc.get(Calendar.WEEK_OF_YEAR));

To get the lYear, lMonth, and lDay values, I am passing a string in the format 04/26/2013 to through the following steps:

String[] arrDate = dateIn.split("/");
int lMonth = Integer.parseInt(arrDate[0]) - 1;
Log.d("SaveHandler", "Month is: " + lMonth);
int lDay = Integer.parseInt(arrDate[1]);
Log.d("SaveHandler", "Day is: " + lDay);
int lYear = Integer.parseInt(arrDate[2]);
Log.d("SaveHandler", "Year is: " + lYear);

The problem I am facing is that when I look at what is outputed to wiy, it is always 1. Upon some further debugging, I realized that the time is being left at epoch time, and not setting to the values I need.

I also tried using lc.set(lYear, lMonth, lDay), also to no avail. If anyone has any ideas, I would greatly appreciate them.

*EDIT: I did some debugging earlier and it is returning 1970 for the year and 0 for the month.

Upvotes: 2

Views: 12355

Answers (2)

Ilya
Ilya

Reputation: 29721

use

formWIM.format(lc.getTime());

instead of

formWIM.format(lc.get(Calendar.WEEK_OF_YEAR));  

EDIT
You can parse your date (instead of dateIn.split( etc.)

  SimpleDateFormat monthDayYear = new SimpleDateFormat("MM/dd/yyyy", Locale.US); //04/26/2013
  Date date = monthDayYear.parse("04/26/2013");  

and then format it

 SimpleDateFormat formWIM = new SimpleDateFormat("ww", Locale.US);  
 formWIM.format(date);

Upvotes: 3

cahen
cahen

Reputation: 16706

This code is correct, the problem is in formWIM.format(...) or the battery of your motherboard clock is drained.

Upvotes: 0

Related Questions