Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.Calendar;

public class Main {
    public static Calendar createCalendarByDateTimeString(String dateString, String timeString) {
        final int year = getYear(dateString);
        final int month = getMonth(dateString);
        final int dayOfMonth = getDayOfMonth(dateString);
        final int hour = getHour(timeString);
        final int minute = getMinute(timeString);

        return createCalendar(year, month, dayOfMonth, hour, minute);
    }

    public static int getYear(String dateString) throws IndexOutOfBoundsException {
        return Integer.parseInt(dateString.substring(0, 4));
    }

    public static int getMonth(String dateString) throws IndexOutOfBoundsException {
        return Integer.parseInt(dateString.substring(4, 6)) - 1;
    }

    public static int getDayOfMonth(String dateString) throws IndexOutOfBoundsException {
        return Integer.parseInt(dateString.substring(6));
    }

    public static int getHour(String timeString) throws IndexOutOfBoundsException {
        return Integer.parseInt(timeString.substring(0, 2));
    }

    public static int getMinute(String timeString) throws IndexOutOfBoundsException {
        return Integer.parseInt(timeString.substring(2, 4));
    }

    public static Calendar createCalendar(int hour, int minute) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, minute);
        calendar.set(Calendar.SECOND, 0);
        return calendar;
    }

    public static Calendar createCalendar(int dayOfMonth, int hour, int minute) {
        Calendar calendar = createCalendar(hour, minute);
        calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
        return calendar;
    }

    public static Calendar createCalendar(int month, int dayOfMonth, int hour, int minute) {
        Calendar calendar = createCalendar(dayOfMonth, hour, minute);
        calendar.set(Calendar.MONTH, month);
        return calendar;
    }

    public static Calendar createCalendar(int year, int month, int dayOfMonth, int hour, int minute) {
        Calendar calendar = createCalendar(month, dayOfMonth, hour, minute);
        calendar.set(Calendar.YEAR, year);
        return calendar;
    }
}