java date get month

htt‮sp‬://www.lautturi.com
java date get month
/**
 * @author lautturi.com 
 * Java example: get month and year from java date
 */

import java.util.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;

public class Lautturi {

	public static void main(String[] args) throws ParseException {

		String datetime = "2020-11-03 12:34:56";
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = format.parse(datetime);
		SimpleDateFormat df = new SimpleDateFormat("MM");
		// SimpleDateFormat df = new SimpleDateFormat("yyyy"); // get year
		String month = df.format(date);

	    System.out.println("The month is: " +  month);
	
	}
}

output:

The month is: 11
Created Time:2017-09-24 19:11:49  Author:lautturi