# Converting String month to int month

Page 1 of 1

## 4 Replies - 25107 Views - Last Post: 16 February 2009 - 02:31 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=86922&amp;s=eeed07bceb8fa6d4709dee50f18b9e0a&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

Reputation: 0
• Posts: 3
• Joined: 16-February 09

# Converting String month to int month

Posted 16 February 2009 - 12:35 AM

I'm practicing on writing code to calculate how many milliseconds one's lived by far.

I got stucked on the part where I'm creating a method to convert(or compare) String month to int month.

And I wish to to use the equals method from the String class to compare to Strings for equality.

I don't know where to start.

Thanks.

This is what I have so far (the entire thing)...not even sure if the rest if right.

```import java.util.Scanner;
import java.util.Calendar;
import java.lang.String;

public class Birthday {

private static String month;
private static int monthInt;

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

System.out.println("In which year were you born?");
int year = scan.nextInt();

System.out.println("In which month were you born?");
String month = scan.next();

System.out.println("On what day of that month were you born?");
int date = scan.nextInt();

int monthInt = monthStringToInt(month); // month as an int.

// I put a little test here for the conversion b/w String month and int month...doesn't seem to work.
System.out.println(monthInt);

Calendar birthmonth = Calendar.getInstance();
birthmonth.set(Calendar.MONTH, monthInt);

Calendar birthyear = Calendar.getInstance();
birthyear.set( Calendar.YEAR, year );

Calendar birthdate = Calendar.getInstance();
birthdate.set( Calendar.DATE, date );

Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
cal2.set(year, monthInt, date);
cal1.set(Calendar.YEAR, Calendar.MONTH, Calendar.DATE);
long milis1 = cal1.getTimeInMillis();
long milis2 = cal2.getTimeInMillis();
long diff = milis2 - milis1;
System.out.println(diff);

}

private static int monthStringToInt(String month) {

int January = Integer.valueOf (Calendar.JANUARY);
if (month.equals(January ))
{
monthInt = January;
}
//Similar to other 11 months

return monthInt;

}
}
```

This post has been edited by MmeLionHead: 16 February 2009 - 02:24 AM

Is This A Good Question/Topic? 0

## Replies To: Converting String month to int month

### #2 mostyfriedman

• The Algorithmi

Reputation: 729
• Posts: 4,473
• Joined: 24-October 08

## Re: Converting String month to int month

Posted 16 February 2009 - 12:37 AM

### #3 ayman_mastermind

• human.setType("geek");

Reputation: 127
• Posts: 1,860
• Joined: 12-December 08

## Re: Converting String month to int month

Posted 16 February 2009 - 12:41 AM

to convert from string to integer you can use:
``` int number = Integer.valueOf(MyNumberInString)
```

or to convert number to string you can use:
```String MyNumberInString = String.valueOf(number),

```

to compare a string to another you can use the equals method for instace:
```if(S1.equals(S2)){
//your code for the condition goes here
}

```

We could help youmore if you show us your code and where you have reached so far, and don't forget to post code using code tags
hope this helps, good luck and thanks

This post has been edited by ayman_mastermind: 16 February 2009 - 12:46 AM

Reputation: 0
• Posts: 3
• Joined: 16-February 09

## Re: Converting String month to int month

Posted 16 February 2009 - 02:21 AM

I've posted my code. Can someone help me please. Thanks.

### #5 mostyfriedman

• The Algorithmi

Reputation: 729
• Posts: 4,473
• Joined: 24-October 08

## Re: Converting String month to int month

Posted 16 February 2009 - 02:31 AM

```	if (month.equals("January"))
{
monthInt = Calendar.JANUARY;
}
//Similar to other 11 months

return monthInt;

}

```

try this out

This post has been edited by mostyfriedman: 16 February 2009 - 02:33 AM