Example usage for java.time YearMonth lengthOfMonth

List of usage examples for java.time YearMonth lengthOfMonth

Introduction

In this page you can find the example usage for java.time YearMonth lengthOfMonth.

Prototype

public int lengthOfMonth() 

Source Link

Document

Returns the length of the month, taking account of the year.

Usage

From source file:Main.java

public static void main(String[] args) {
    YearMonth y = YearMonth.now();

    System.out.println(y.lengthOfMonth());

}

From source file:Main.java

public static void main(String[] args) {
    for (Month month : Month.values()) {
        YearMonth ym = YearMonth.of(2014, month);
        System.out.printf("%s: %d days%n", month, ym.lengthOfMonth());
    }//from  www . ja v a 2s  .c o m
}

From source file:Main.java

public static void main(String[] argv) {
    YearMonth currentYearMonth = YearMonth.now();
    System.out.printf("Days in month year %s: No of days: %s \n", currentYearMonth,
            currentYearMonth.lengthOfMonth());
    YearMonth creditCardExpiry = YearMonth.of(2018, Month.FEBRUARY);
    System.out.printf("Your credit card expires on %s: No of days: %s \n", creditCardExpiry,
            creditCardExpiry.lengthOfMonth());

}

From source file:MonthsInYear.java

public static void main(String[] args) {
    int year = 0;

    if (args.length <= 0) {
        System.out.printf("Usage: MonthsInYear <year>%n");
        throw new IllegalArgumentException();
    }/*from w  w w .  j a v a  2 s.  c o  m*/

    try {
        year = Integer.parseInt(args[0]);
    } catch (NumberFormatException nexc) {
        System.out.printf("%s is not a properly formatted number.%n", args[0]);
        throw nexc; // Rethrow the exception.
    }

    try {
        Year test = Year.of(year);
    } catch (DateTimeException exc) {
        System.out.printf("%d is not a valid year.%n", year);
        throw exc; // Rethrow the exception.
    }

    System.out.printf("For the year %d:%n", year);
    for (Month month : Month.values()) {
        YearMonth ym = YearMonth.of(year, month);
        System.out.printf("%s: %d days%n", month, ym.lengthOfMonth());
    }
}

From source file:com.mycompany.flooringmaster.Controllers.Controller.java

private String getFileName(boolean acceptAnything, String date) {
    DateFormat dateFormat = new SimpleDateFormat("MMddyyyy");
    Date currentDate = new Date();
    String strDate = "Orders_" + dateFormat.format(currentDate) + ".txt";
    DateFormat yearFormat = new SimpleDateFormat("YYYY");
    int currentYear = Integer.parseInt(yearFormat.format(currentDate));
    boolean notValid = true, answer = true;

    while (notValid) {
        try {//  ww  w  .j  a  v  a 2s.co  m
            String response = io.getUserInputString("Use today's date for the order? (y/n): ");
            if (acceptAnything && response.equals("")) {
                return date;
            } else if (response.equalsIgnoreCase("yes") || response.equalsIgnoreCase("y")) {
                answer = true;
                notValid = false;
            } else if (response.equalsIgnoreCase("no") || response.equalsIgnoreCase("n")) {
                answer = false;
                notValid = false;
            } else {
                notValid = true;
            }
        } catch (Exception ex) {
        }
    }

    if (!answer) {
        String fullDate;
        int year, month, day;
        YearMonth yearMonthObject;
        boolean valid = false;

        do {
            do {
                fullDate = io.getUserInputString("Date (MMDDYYYY): ");
                fullDate = fullDate.replaceAll("[^\\d]", "");
            } while (fullDate.length() != 8);
            month = Integer.parseInt(fullDate.substring(0, 2));
            day = Integer.parseInt(fullDate.substring(2, 4));
            year = Integer.parseInt(fullDate.substring(4, 8));
            yearMonthObject = YearMonth.of(year, month);
            if (day > yearMonthObject.lengthOfMonth() || day < 1) {
                valid = false;
            } else if (month > 12 || month < 1) {
                valid = false;
            } else if (year > currentYear || year < 2000) {
                valid = false;
            } else {
                valid = true;
            }
        } while (!valid);
        strDate = "Orders_" + String.format("%02d", month) + String.format("%02d", day) + year + ".txt";
    }
    return strDate;
}