How to find out number of days elapsed between two days. I know that if hours elapsed between two days is above 24, its the next day. But this is not always the case for e.g.
I know this has been covered before but none of the answers made sense to me. I'm hoping there is an easy way to do this. I have 2 user inputted strings that I have converted to dates and I just want the difference in days.
My code :
import java.util.Date; import java.text.SimpleDateFormat; import java.util.Scanner; import java.text.ParseException; public class DateTest { /** * @param args the command line arguments */ public static void main(String[] args) throws ParseException {
[Code] ....
Apparently I can't just subtract the dates like I would in VB.
I need to create a simple Java program that calculates the difference between two entered days of the week with the following requirements:
-Ask the user to enter in a day of the week by name (e.g. Monday) - Ask the user to enter in another day of the week by name (e.g. Friday) - Create a static method other than main that takes in two Strings as input, and returns an integer value equal to the difference between the two days - Use an array with the days of the week in it to find each entered in day of the week, and use their indices to calculate the difference - Output the difference for the user to see
import java.util.Scanner; import java.io.*; public class LeapYear { public static void main(String[] args) throws IOException { int month1, day1, year1, month2, day2, year2;
[Code] ....
This is the compile error I'm getting:
The total number of days between the dates you entered are: 76882 Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:909) at java.util.Scanner.next(Scanner.java:1530) at java.util.Scanner.nextInt(Scanner.java:2160) at java.util.Scanner.nextInt(Scanner.java:2119) at LeapYear.main(LeapYear.java:16)
I'm trying to write a program that computes the number of days between two dates. How I can achieve this. I'm quite sure I'm supposed to use the Calendar class.
I'm supposed to calculate and display the number of total days, and then the number of months, and days from now (when the program is run) to when the person's next birthday is (no leap year). Here is my code:
import java.util.Scanner; import java.text.*; import java.lang.System; import java.util.Date; import java.util.Calendar; public class CSCD210Lab3 { public static void main (String [] args)
[Code] .....
When I call daysFromMillis in the print statement, if I enter in a birthday of 02/17/1993, I will get an answer of "our birthday is 5435 days from now."
I need to add a few days to make an end date (for instance the start date is the first date selected, and then the end date is x amount of days after that... I think I should be using the add() method but I am not quite sure of where or how to do so)...
Java Code:
//Get an instance of Calendar and DateFormat class, and get current date from the system using OnClick method Calendar startVacation = Calendar.getInstance(); DateFormat fmtDate = DateFormat.getDateInstance(); DatePickerDialog.OnDateSetListener startVaca = new DatePickerDialog.OnDateSetListener() {
I am assigned with a project that will as the user for the amount of days they would like data entered, then loop asking them for these numbers. once it reaches the entered amount of days it will display a menu for which the user can enter the number they want on the list and it will display the data. the menu has 6 items. Total, Average, Determine if its a Drought, Flood or normal rainfall, highest of data entered, lowest of data entered, and 6 is to quit the program.
So far i have total and average working properly. Still looking at how to display drought, flood, and normal.
Main problems, i cannot get highest and lowest to display correct data.
Also when entering 6, or an invalid menu choice, neither "goodbye" or "invalid choice, choose again" gets printed. Rather the loop for entering data continues.
import java.util.Scanner; public class RainAnalysis { /* * Project 1 This program will analyze the rainfall for the number of days a user inputs. */
public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); int numOfDays = 0; double rainfallAmount = 0.0;
[Code] ....
Here is a sample output to show what happens when i put in 6 after the menu is displayed.
also i noticed that it does not say the correct day when displaying the lowest and highest rainfall. this is because i put count, i did not know what else to put in order to get the correct day.
how many days do you want to monitor rainfall?5
Enter the rainfall for day 1.01 Enter the rainfall for day 2.1 Enter the rainfall for day 36 Enter the rainfall for day 43 Enter the rainfall for day 51
Rainfall Data Display Menu: 1 - Display total rainfall. 2 - Display rain average for the period. 3 - Display if drought, flood, or normal conditions. 4 - Display the day and amount of the highest rainfall. 5 - Display the day and amount of the lowest rainfall. 6 - Quit the program.
Enter your choice: 4 Day5 has the highest rainfall with 6.0 inches.
Rainfall Data Display Menu: 1 - Display total rainfall. 2 - Display rain average for the period. 3 - Display if drought, flood, or normal conditions. 4 - Display the day and amount of the highest rainfall. 5 - Display the day and amount of the lowest rainfall. 6 - Quit the program.
Enter your choice: 5 Day5 has the lowest rainfall with 0.1 inches.
Rainfall Data Display Menu: 1 - Display total rainfall. 2 - Display rain average for the period. 3 - Display if drought, flood, or normal conditions. 4 - Display the day and amount of the highest rainfall. 5 - Display the day and amount of the lowest rainfall. 6 - Quit the program.
So I've attempted a for loop for this type of problem:If you receive $0.01 on the first day of your allowance, and the daily rate doubles each day after, calculate the total earnings for 30 days.
Output should be like: Day ----- Daily allowance - Total allowance 1 -------- 0.01 -------------- 0.01 2 -------- 0.02 -------------- 0.03 3 -------- 0.04 -------------- 0.07
My code so far:
public class Allowance { public static void main (String[] args) { int day = 0; double daily = 0.0; double total = 0.1; System.out.println("Day Daily Allowance Total Allowance"); for(day = 0; day <= 30; )
The instructions state: Create the following values for the enum (and make sure that they are spelled correctly, case matters):
a. SUN b. MON c. TUE d. WED e. THU f. FRI g. SAT
Create a private constructor for the Day enum that takes a boolean value. This boolean will determine if the day is a weekday (if it is true) or weekend (if it is false). Document it with a JavaDoc. This value will need to be stored as part of the object. Update the enum values so that they correctly call the constructor. Saturday and Sunday are the only days considered part of the weekend.
Implement the following methods: a. boolean isWeekday() i. Returns true if the day is a weekday.
b. Boolean isWeekend() i. Returns true if the day is a weekend. (How could you determine that?)
c. String toString() i. Returns the full name of the day for the enum value (i.e. Monday or Tuesday). Use either a set of nested ifs or a switch statement.
Hint: You will want to compare the this reference against the possible values in the enum.
How to get used to program control statements. This is an attempt to write a program that returns the number of days between two dates. Sadly, it give incorrect values.
public class DaysBetweenDates { public static boolean isLeapyear(int year) {//This method will determine if a year is a leap year or not boolean isTrue=false; if(year%4==0 && year%100!=0) isTrue=true; else if(year%400==0) isTrue=true; return isTrue;
So basically we have this question to do : Write a method dayNumber that determines the number of days in a year up to and including the current day. The method should have three int parameters: year, month, and day. If the value of any parameter is invalid, the method should print a warning message and return the value zero. The table gives some examples of the action of the method. Accept any non-negative year as being valid. You may want to assume the existence of a method numberOfDays that returns the number of days in a given month of a given year. And you should have a method call isLeapYear, if the user enter a year that is a leap year.
This is what I did so far:
class dayMonthYear { public static void main(String[] args) { System.out.println("Enter a year"); int year = In.getInt(); System.out.println("Enter the month for '1' to be January - '12' to be december");
[Code] ....
It works and compiles but my problem is that: let say I enter "12" for December and December has 31 days, so what my program will do since December has 31 days, it thinks each month has 31 days and add them up which will give me 372 when it's suppose to give me 365. How do I make it that it won't do that and that it will work if the year is a leap year too.
import java.util.Scanner; public class days { public EnumTest () { this.day = weeks/months/years} final int daysInMonth = 30; //constants final int monthsInYear = 12; final int daysInWeek = 7; public void convert()
[Code] ....
after compiling it shows invalid method declaration ; return type required.
I need to write a simple program that reads an amount of minutes and displays the approximate number of years and days for the minutes. For simplicity, assume a year has 365 days and the resulting amount of days is a whole number.
I've been working on a question using parallel arrays where the user inputs an integer 1-12 and the output will be the name of the month and the number of days in that month. This is what I have so far
import java.util.*; public class DaysMonth { public static void main (String args[]) { Scanner keyIn = new Scanner(System.in); int[] days = new int[]{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
I am new to java i dont understand the difference between the abstract and inheritance i mean we use the abstract class with extends with other class name ,even we do that same in the inheritance pls tell me main difference between abstract and inheritance...
I have been tasked with creating an invoice (school assignment). Part of the calculations is creating an interest depending on the amount of days between the current date entered, and invoice date entered (The program prompts the user to enter both the current and invoice dates by asking for the day, month and year).
We are also supposed to set the contructor to a default date of 1/1/1900.. but I don't know how or where to code that.
How to calculate the difference between the CurrentDate and Invoice. I have displayed the dates to the user as follows.
I have just begin to understand collections in Java. I read about the differences between LinkedList and ArrayList but have several doubts on it. I have mentioned them below
1) Why retrieving in ArrayList is faster then LinkedList?. Since both have the get method how does performance differ?.
2) How does re-sizing happens internally in ArrayList when a item is added or removed?. How slow this is compared to the pointer re-shuffling in LinkedList when a item is added or removed?.
I read that InputStream is used for byte based reading it reads 1 byte at a time.And InputStreamReader is used for charcter based reading so it reads one charcter at a time so no need to convert this first to int then read it.Here is reading using InputStream.
input=new FileInputStream("D:/input.txt"); int c; while((c=input.read())!=-1) { System.out.print((char)c); }
and here is reading using InputStreamReader
input=new FileInputStream("D:/input.txt"); reader=new InputStreamReader(input,"UTF-8"); int c;
so what is difference between InputStream and InputStreamReader in both case i have to use a Int and then read it and at the end if I want to print that data I have to cast that with "(char)c".So what is advantage of using InputStreamReader?