get Date From Year And Month String - Java java.time

Java examples for java.time:Month

Description

get Date From Year And Month String

Demo Code


//package com.java2s;
import java.time.LocalDate;

public class Main {


    public static LocalDate getDateFromYearAndMonthString(String year,
            String month) {/* w ww. j  av a2 s .com*/
        int yearInt = Integer.parseInt(year);
        int monthInt = monthStringToInt(month);
        return LocalDate.of(yearInt, monthInt, monthInt != 2 ? 30 : 28);
    }

    public static int monthStringToInt(String dateMonthStart) {
        if (dateMonthStart.isEmpty())
            return 6;
        switch (dateMonthStart.toLowerCase().substring(0, 3)) {
        case "jan":
            return 1;
        case "feb":
            return 2;
        case "mar":
            return 3;
        case "apr":
            return 4;
        case "may":
            return 5;
        case "jun":
            return 6;
        case "jul":
            return 7;
        case "aug":
            return 8;
        case "sep":
            return 9;
        case "oct":
            return 10;
        case "nov":
            return 11;
        case "dec":
            return 12;
        default:
            return 1;
        }
    }
}

Related Tutorials