189 lines
17 KiB
HTML
189 lines
17 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
|
<html>
|
|
<head>
|
|
<title>PrintCalendar.java</title>
|
|
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
|
|
<style type="text/css">
|
|
<!--
|
|
body {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
|
|
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
|
|
table {color: #888888; background-color: #313335; font-family: monospace; font-weight: bold}
|
|
.literal {color: #cc7832}
|
|
.ST3 {font-family: monospace; font-weight: bold; font-style: italic}
|
|
.ST0 {color: #287bde}
|
|
.string {color: #6a8759}
|
|
.number {color: #6897bb}
|
|
.comment {color: #808080}
|
|
.whitespace {color: #505050}
|
|
.ST1 {color: #ffc66d; font-family: monospace; font-weight: bold; font-style: italic}
|
|
.ST2 {color: #9876aa; font-family: monospace; font-weight: bold; font-style: italic}
|
|
-->
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 2/Assignments/MP5_CalebFontenot/src/main/java/com/calebfontenot/mp5_calebfontenot/PrintCalendar.java</td></tr></table>
|
|
<pre>
|
|
<span class="comment">/*</span>
|
|
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt</span><span class="comment"> to change this license</span>
|
|
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java</span><span class="comment"> to edit this template</span>
|
|
<span class="comment"> */</span>
|
|
<span class="literal">package</span> com.calebfontenot.mp5_calebfontenot;
|
|
|
|
<span class="comment">/**</span>
|
|
<span class="comment"> *</span>
|
|
<span class="comment"> * </span><span class="comment">@author</span> <span class="comment">caleb</span>
|
|
<span class="comment">*/</span>
|
|
<span class="literal">import</span> java.util.Calendar;
|
|
<span class="literal">import</span> java.util.Scanner;
|
|
<span class="literal">import</span> java.util.Date;
|
|
<span class="literal">import</span> java.util.GregorianCalendar;
|
|
|
|
<span class="literal">public</span> <span class="literal">class</span> PrintCalendar {
|
|
<span class="comment">/**</span> <span class="comment">Main</span> <span class="comment">method</span> <span class="comment">*/</span>
|
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST1">main</span>(String[] args) {
|
|
Scanner input = <span class="literal">new</span> Scanner(System.<span class="ST2">in</span>);
|
|
<span class="literal">int</span> year, month;
|
|
String userInput;
|
|
|
|
<span class="literal">do</span> {
|
|
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">What would you like to do?</span><span class="string">"</span>);
|
|
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">1. Print today's date.</span><span class="string">"</span>);
|
|
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">2. Print a specified date.</span><span class="string">"</span>);
|
|
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">Q/q. Quit.</span><span class="string">"</span>);
|
|
System.<span class="ST2">out</span>.print(<span class="string">"</span><span class="string">Respond with 1, 2 or Q/q: </span><span class="string">"</span>);
|
|
userInput = input.next();
|
|
<span class="literal">if</span> (userInput.toLowerCase().charAt(<span class="number">0</span>) == <span class="string">'</span><span class="string">q</span><span class="string">'</span>) {
|
|
System.<span class="ST3">exit</span>(<span class="number">0</span>);
|
|
}
|
|
<span class="literal">if</span> (Integer.<span class="ST3">parseInt</span>(userInput) < <span class="number">0</span> || Integer.<span class="ST3">parseInt</span>(userInput) > <span class="number">2</span>) {
|
|
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">Invalid input!</span><span class="string">"</span>);
|
|
} <span class="literal">else</span> {
|
|
<span class="literal">if</span> (Integer.<span class="ST3">parseInt</span>(userInput) == <span class="number">1</span>) {
|
|
Date date = <span class="literal">new</span> Date();
|
|
<span class="ST3">printMonth</span>((date.getYear() + <span class="number">1900</span>), date.getMonth());
|
|
} <span class="literal">else</span> <span class="literal">if</span> (Integer.<span class="ST3">parseInt</span>(userInput) == <span class="number">2</span>) {
|
|
System.<span class="ST2">out</span>.print(<span class="string">"</span><span class="string">Enter a month (1-12): </span><span class="string">"</span>);
|
|
month = (input.nextInt() + <span class="number">1</span>);
|
|
System.<span class="ST2">out</span>.print(<span class="string">"</span><span class="string">Enter a year: </span><span class="string">"</span>);
|
|
year = (input.nextInt());
|
|
GregorianCalendar date = <span class="literal">new</span> GregorianCalendar(year, month, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>);
|
|
<span class="ST3">printMonth</span>(date.get(Calendar.<span class="ST2">YEAR</span>), date.get(Calendar.<span class="ST2">MONTH</span>));
|
|
}
|
|
}
|
|
} <span class="literal">while</span> (<span class="literal">true</span>);
|
|
|
|
}
|
|
|
|
<span class="comment">/**</span> <span class="comment">Print</span> <span class="comment">the</span> <span class="comment">calendar</span> <span class="comment">for</span> <span class="comment">a</span> <span class="comment">month</span> <span class="comment">in</span> <span class="comment">a</span> <span class="comment">year</span> <span class="comment">*/</span>
|
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST1">printMonth</span>(<span class="literal">int</span> year, <span class="literal">int</span> month) {
|
|
<span class="comment">// Print the headings of the calendar</span>
|
|
<span class="ST3">printMonthTitle</span>(year, month);
|
|
|
|
<span class="comment">// Print the body of the calendar</span>
|
|
<span class="ST3">printMonthBody</span>(year, month);
|
|
}
|
|
|
|
<span class="comment">/**</span> <span class="comment">Print</span> <span class="comment">the</span> <span class="comment">month</span> <span class="comment">title</span><span class="comment">, </span><span class="comment">e</span><span class="comment">.</span><span class="comment">g</span><span class="comment">.</span><span class="comment">, </span><span class="comment">May</span><span class="comment">, 1999 </span><span class="comment">*/</span>
|
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST1">printMonthTitle</span>(<span class="literal">int</span> year, <span class="literal">int</span> month) {
|
|
System.<span class="ST2">out</span>.println(<span class="string">"</span> <span class="string">"</span> + <span class="ST3">getMonthName</span>(month)
|
|
+ <span class="string">"</span> <span class="string">"</span> + year);
|
|
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">-----------------------------</span><span class="string">"</span>);
|
|
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string"> Sun Mon Tue Wed Thu Fri Sat</span><span class="string">"</span>);
|
|
}
|
|
|
|
<span class="comment">/**</span> <span class="comment">Get</span> <span class="comment">the</span> <span class="comment">English</span> <span class="comment">name</span> <span class="comment">for</span> <span class="comment">the</span> <span class="comment">month</span> <span class="comment">*/</span>
|
|
<span class="literal">public</span> <span class="literal">static</span> String <span class="ST1">getMonthName</span>(<span class="literal">int</span> month) {
|
|
String monthName = <span class="string">""</span>;
|
|
<span class="literal">switch</span> (month) {
|
|
<span class="literal">case</span> <span class="number">1</span>: monthName = <span class="string">"</span><span class="string">January</span><span class="string">"</span>; <span class="literal">break</span>;
|
|
<span class="literal">case</span> <span class="number">2</span>: monthName = <span class="string">"</span><span class="string">February</span><span class="string">"</span>; <span class="literal">break</span>;
|
|
<span class="literal">case</span> <span class="number">3</span>: monthName = <span class="string">"</span><span class="string">March</span><span class="string">"</span>; <span class="literal">break</span>;
|
|
<span class="literal">case</span> <span class="number">4</span>: monthName = <span class="string">"</span><span class="string">April</span><span class="string">"</span>; <span class="literal">break</span>;
|
|
<span class="literal">case</span> <span class="number">5</span>: monthName = <span class="string">"</span><span class="string">May</span><span class="string">"</span>; <span class="literal">break</span>;
|
|
<span class="literal">case</span> <span class="number">6</span>: monthName = <span class="string">"</span><span class="string">June</span><span class="string">"</span>; <span class="literal">break</span>;
|
|
<span class="literal">case</span> <span class="number">7</span>: monthName = <span class="string">"</span><span class="string">July</span><span class="string">"</span>; <span class="literal">break</span>;
|
|
<span class="literal">case</span> <span class="number">8</span>: monthName = <span class="string">"</span><span class="string">August</span><span class="string">"</span>; <span class="literal">break</span>;
|
|
<span class="literal">case</span> <span class="number">9</span>: monthName = <span class="string">"</span><span class="string">September</span><span class="string">"</span>; <span class="literal">break</span>;
|
|
<span class="literal">case</span> <span class="number">10</span>: monthName = <span class="string">"</span><span class="string">October</span><span class="string">"</span>; <span class="literal">break</span>;
|
|
<span class="literal">case</span> <span class="number">11</span>: monthName = <span class="string">"</span><span class="string">November</span><span class="string">"</span>; <span class="literal">break</span>;
|
|
<span class="literal">case</span> <span class="number">12</span>: monthName = <span class="string">"</span><span class="string">December</span><span class="string">"</span>;
|
|
}
|
|
|
|
<span class="literal">return</span> monthName;
|
|
}
|
|
|
|
<span class="comment">/**</span> <span class="comment">Print</span> <span class="comment">month</span> <span class="comment">body</span> <span class="comment">*/</span>
|
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST1">printMonthBody</span>(<span class="literal">int</span> year, <span class="literal">int</span> month) {
|
|
<span class="comment">// Get start day of the week for the first date in the month</span>
|
|
<span class="literal">int</span> startDay = <span class="ST3">getStartDay</span>(year, month);
|
|
|
|
<span class="comment">// Get number of days in the month</span>
|
|
<span class="literal">int</span> numberOfDaysInMonth = <span class="ST3">getNumberOfDaysInMonth</span>(year, month);
|
|
|
|
<span class="comment">// Pad space before the first day of the month</span>
|
|
<span class="literal">int</span> i = <span class="number">0</span>;
|
|
<span class="literal">for</span> (i = <span class="number">0</span>; i < startDay; i++)
|
|
System.<span class="ST2">out</span>.print(<span class="string">"</span> <span class="string">"</span>);
|
|
|
|
<span class="literal">for</span> (i = <span class="number">1</span>; i <= numberOfDaysInMonth; i++) {
|
|
System.<span class="ST2">out</span>.printf(<span class="string">"</span><span class="string">%4d</span><span class="string">"</span>, i);
|
|
|
|
<span class="literal">if</span> ((i + startDay) % <span class="number">7</span> == <span class="number">0</span>)
|
|
System.<span class="ST2">out</span>.println();
|
|
}
|
|
|
|
System.<span class="ST2">out</span>.println();
|
|
}
|
|
|
|
<span class="comment">/**</span> <span class="comment">Get</span> <span class="comment">the</span> <span class="comment">start</span> <span class="comment">day</span> <span class="comment">of</span> <span class="comment">month</span><span class="comment">/1/</span><span class="comment">year</span> <span class="comment">*/</span>
|
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">int</span> <span class="ST1">getStartDay</span>(<span class="literal">int</span> year, <span class="literal">int</span> month) {
|
|
<span class="literal">final</span> <span class="literal">int</span> START_DAY_FOR_JAN_1_1800 = <span class="number">3</span>;
|
|
<span class="comment">// Get total number of days from 1/1/1800 to month/1/year</span>
|
|
<span class="literal">int</span> totalNumberOfDays = <span class="ST3">getTotalNumberOfDays</span>(year, month);
|
|
|
|
<span class="comment">// Return the start day for month/1/year</span>
|
|
<span class="literal">return</span> (totalNumberOfDays + START_DAY_FOR_JAN_1_1800) % <span class="number">7</span>;
|
|
}
|
|
|
|
<span class="comment">/**</span> <span class="comment">Get</span> <span class="comment">the</span> <span class="comment">total</span> <span class="comment">number</span> <span class="comment">of</span> <span class="comment">days</span> <span class="comment">since</span> <span class="comment">January</span><span class="comment"> 1, 1800 </span><span class="comment">*/</span>
|
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">int</span> <span class="ST1">getTotalNumberOfDays</span>(<span class="literal">int</span> year, <span class="literal">int</span> month) {
|
|
<span class="literal">int</span> total = <span class="number">0</span>;
|
|
|
|
<span class="comment">// Get the total days from 1800 to 1/1/year</span>
|
|
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">1800</span>; i < year; i++)
|
|
<span class="literal">if</span> (<span class="ST3">isLeapYear</span>(i))
|
|
total = total + <span class="number">366</span>;
|
|
<span class="literal">else</span>
|
|
total = total + <span class="number">365</span>;
|
|
|
|
<span class="comment">// Add days from Jan to the month prior to the calendar month</span>
|
|
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">1</span>; i < month; i++)
|
|
total = total + <span class="ST3">getNumberOfDaysInMonth</span>(year, i);
|
|
|
|
<span class="literal">return</span> total;
|
|
}
|
|
|
|
<span class="comment">/**</span> <span class="comment">Get</span> <span class="comment">the</span> <span class="comment">number</span> <span class="comment">of</span> <span class="comment">days</span> <span class="comment">in</span> <span class="comment">a</span> <span class="comment">month</span> <span class="comment">*/</span>
|
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">int</span> <span class="ST1">getNumberOfDaysInMonth</span>(<span class="literal">int</span> year, <span class="literal">int</span> month) {
|
|
<span class="literal">if</span> (month == <span class="number">1</span> || month == <span class="number">3</span> || month == <span class="number">5</span> || month == <span class="number">7</span> ||
|
|
month == <span class="number">8</span> || month == <span class="number">10</span> || month == <span class="number">12</span>)
|
|
<span class="literal">return</span> <span class="number">31</span>;
|
|
|
|
<span class="literal">if</span> (month == <span class="number">4</span> || month == <span class="number">6</span> || month == <span class="number">9</span> || month == <span class="number">11</span>)
|
|
<span class="literal">return</span> <span class="number">30</span>;
|
|
|
|
<span class="literal">if</span> (month == <span class="number">2</span>) <span class="literal">return</span> <span class="ST3">isLeapYear</span>(year) ? <span class="number">29</span> : <span class="number">28</span>;
|
|
|
|
<span class="literal">return</span> <span class="number">0</span>; <span class="comment">// If month is incorrect</span>
|
|
}
|
|
|
|
<span class="comment">/**</span> <span class="comment">Determine</span> <span class="comment">if</span> <span class="comment">it</span> <span class="comment">is</span> <span class="comment">a</span> <span class="comment">leap</span> <span class="comment">year</span> <span class="comment">*/</span>
|
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">boolean</span> <span class="ST1">isLeapYear</span>(<span class="literal">int</span> year) {
|
|
<span class="literal">return</span> year % <span class="number">400</span> == <span class="number">0</span> || (year % <span class="number">4</span> == <span class="number">0</span> && year % <span class="number">100</span> != <span class="number">0</span>);
|
|
}
|
|
}
|
|
|
|
|
|
</pre></body>
|
|
</html>
|