Given a month string such as:
"Feb" or "February"
Is there any core Java or third party library functionality that would allow you to convert this string to the corresponding month number in a locale agnostic way?
setTime(date); int month = cal. get(Calendar. MONTH); System.
// displaying month number Format f = new SimpleDateFormat("M"); String strMonth = f. format(new Date()); System. out. println("Month Number = "+strMonth);
Or, if you really want to convert the 'date' into integer type 06/03/2017 to 06032017 .. you can do something like this. SimpleDateFormat sdf = new SimpleDateFormat("ddMMyyyy"); System. out. println(Integer.
Extract the number of years and of months. int years = period. getYears(); int months = period. getMonths();
You could parse the month using SimpleDateFormat:
Date date = new SimpleDateFormat("MMM", Locale.ENGLISH).parse("Feb"); Calendar cal = Calendar.getInstance(); cal.setTime(date); int month = cal.get(Calendar.MONTH); System.out.println(month == Calendar.FEBRUARY);
Be careful comparing int month
to an int (it does not equal 2!). Safest is to compare them using Calendar
's static fields (like Calendar.FEBRUARY
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With