Using Java to calculate a date is a day of the week _java

Source: Internet
Author: User
Tags readline

If you don't say more, look at the sample code directly below

Specific code:

Dayofweek4birthday.java package Com.gua;
Import Java.util.Calendar;
Import Java.util.GregorianCalendar;
Import java.io.*;

Import static java.lang.System.out;
 /** * Created by 2gua on 2014/9/27.
 * Dayofweek4birthday: Look at the specific date of your query is the day of the week, * For example, inquires who "birthday corresponds to the days of the week."

  * * Public class Dayofweek4birthday {private string[] date;//Save input data: Years, months, date.
  Calculates the day of the week for the date data entered.
    private void Caculatedata () {GregorianCalendar GC = new GregorianCalendar ();
    Final char[] Day_of_week = {' Day ', ' one ', ' two ', ' three ', ' four ', ' five ', ' six '}; int year = Gc.get (calendar.year);
    From the current year.

    Char week; for (int i = year; i<= year + integer.valueof (Date[0])-1; i++) {Gc.set (i, integer.valueof (date[1))-1, Integer

      . valueof (date[2]);

      Week = Day_of_week[gc.get (Calendar.day_of_week)-1]; OUT.PRINTLN (i + "year" + date[1] + "month" + date[2] + "number is Week" + Week + ".
    ");
  }//Enter date data.
    private void Inputdata () {InputStreamReader is = new InputStreamReader (system.in); BufferedReader br = new BufferedReader (IS);
      try {out.print ("Enter the year range and date, format: The number of years in a few months (5 9 15), carriage return end:");
      String in = Br.readline ();
      Date = In.split (""); OUT.PRINTLN ("Input result:" + date[0] + "Number of years," + date[1] + "month" + date[2] + ".
    "); catch (Exception e) {out.println ("Ah Oh, run wrong-_-.") sorry!
    ");
        Finally {try {br.close ();
      Is.close (); The catch (IOException e) {out.println ("IO error-_-. sorry!
      "); }} public static void Main (string[] args) {Dayofweek4birthday dayofweek4birthday = new Dayofweek4birthday

    ();
    Dayofweek4birthday.inputdata ();
  Dayofweek4birthday.caculatedata (); }
}

Run, for example, to see from this year 5 years, the September 15 of the year is the day of the week.

The results are as follows:

Enter the year range and date, format: Number of years in a few months (5 9 15), enter end: 5 9
Input results: 5 years, September 15.
September 15, 2014 is Monday.
September 15, 2015 is Tuesday.
September 15, 2016 is Thursday.
September 15, 2017 is Friday.
September 15, 2018 is Saturday.

Process finished with exit code 0

Remember to close with the flow.

This is the practice of JDK 6, where you can use the new features in JDK 7 and JDK 8 to refactor the automatic resource management(ARM) inputData() method:

Enter date data.
private void Inputdata () {
  try (bufferedreader br = new BufferedReader (new InputStreamReader (system.in))) { C17/>out.print ("Please enter the year range and date, format: Number of years in a few months (5 9 15), carriage return end:");
    String in = Br.readline ();
    Date   = In.split ("");
    OUT.PRINTLN ("Input result:" + date[0] + "Number of years," + date[1] + "month" + date[2] + ". ");
  }
  catch (Exception e) {
    out.println ("Ah Oh, run wrong-_-.") sorry! ");
  }
}

Of course, remember to set the module language level accordingly to JDK 7 or JDK 8,JDK 6 and below will not pass.

Summarize

The above is the entire content of this article, I hope the content of this article for everyone to learn or use Java can help, if you have questions you can message exchange.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.