Monday, August 22, 2022

Convert String Date of Month To number In Java

Input :  String Month : March 

Output : 3

package com.javaiq.in;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class MonthConvert {

	public static void main(String[] args) throws ParseException {
		SimpleDateFormat month_date = new SimpleDateFormat("M", Locale.ENGLISH);
		SimpleDateFormat sdf = new SimpleDateFormat("MMMM");
		String monthStr = "March";
		Date date = sdf.parse(monthStr);
		String month_name = month_date.format(date);
		System.out.println("Month :  " + Integer.parseInt(month_name));  //Mar 2016
	}
}
Month :3

No comments:

Post a Comment