Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.Calendar;
import java.util.Date;

public class Main {
    public static int getMonth() {
        return (getCalendar(getCurrentDate()).get(Calendar.MONTH) + 1);
    }

    public static int getMonth(Date date) {
        return (getCalendar(date).get(Calendar.MONTH) + 1);
    }

    public static String getMonth(Date date, boolean withZeroFill) {
        Calendar calendar = getCalendar(date);
        String month = (calendar.get(Calendar.MONTH) + 1) + "";
        return (month.length() == 1 && withZeroFill) ? ("0" + month) : month;
    }

    public static Calendar getCalendar(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar;
    }

    public static Date getCurrentDate() {
        return new Date(System.currentTimeMillis());
    }
}