英文:
How can i calculate the days between two user inputs that are to be determined once entered?
问题
我想计算周期的开始日期和结束日期之间的天数。用户将输入这些日期。我希望能够使用输入来计算周期的持续时间,即numberofdays
。
以下是我的当前代码和我希望它运行的基本思路。我看过类似的帖子,但没有涉及使用未知用户日期的情况。任何帮助将不胜感激,谢谢。
import java.util.Scanner;
import java.text.SimpleDateFormat;
import java.util.Date;
public class project1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String CustomersName = "";
System.out.print("请输入您的姓名 ==> ");
CustomersName = scanner.nextLine();
Date thisDate = new Date();
SimpleDateFormat dateForm = new SimpleDateFormat("dd/MM/Y");
System.out.println("今天的日期: " + dateForm.format(thisDate));
System.out.println();
System.out.println("输入周期开始日期: ");
String startDate = scanner.next();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/Y");
System.out.println();
System.out.println("输入周期结束日期: ");
String endDate = scanner.next();
SimpleDateFormat endDateFormat = new SimpleDateFormat("dd/MM/Y");
System.out.println("\n\n--- 女性日历结果 ---");
System.out.println();
System.out.println("客户姓名: " + CustomersName);
System.out.println("周期持续天数: " + calculateDays(startDate, endDate));
System.out.println("下一个周期大约在: " + calculateNextCycle(startDate));
scanner.close();
}
// 计算周期持续天数
public static long calculateDays(String startDate, String endDate) {
try {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/Y");
Date start = dateFormat.parse(startDate);
Date end = dateFormat.parse(endDate);
long diffInMillies = Math.abs(end.getTime() - start.getTime());
long diffInDays = diffInMillies / (24 * 60 * 60 * 1000);
return diffInDays;
} catch (Exception e) {
return -1; // 如果出现错误,返回-1表示计算失败
}
}
// 计算下一个周期的开始日期
public static String calculateNextCycle(String startDate) {
try {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/Y");
Date start = dateFormat.parse(startDate);
Calendar calendar = Calendar.getInstance();
calendar.setTime(start);
calendar.add(Calendar.DATE, 28); // 28天后是下一个周期的开始
Date nextCycleDate = calendar.getTime();
return dateFormat.format(nextCycleDate);
} catch (Exception e) {
return "计算失败";
}
}
}
请注意,我添加了两个新的方法,calculateDays
用于计算周期的持续天数,calculateNextCycle
用于计算下一个周期的开始日期。这些方法将根据输入的日期计算相应的值。
英文:
I am wanting to calculate the days between the start date of cycle and end date of cycle. the user will input the dates. i want to be able to use that input to calculate how long the cycle last as numberofdays.
the following is my current code and basic idea of how i want it to run. I have seen similar posts but nothing towards using an unknown user date. Any help would be appreciated thank you.
import java.util.Scanner;
import java.util.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
import javax.swing.JOptionPane;
import javax.swing.*;
import java.awt.*;
import java.time.*;
import java.util.Calendar;
public class project1
{
public static void main (String [] args)
{
int StartDate = 0; //Declaring Number of Toppings
int EndDate = 0 ; //Declaring Charge Per Topping in decimal
Scanner inNumber= new Scanner (System.in); //Crate Scanner for input of number of toppings
Scanner inText = new Scanner(System.in); //Creat Scanner for input of Name of Customers
System.out.println(" Female Calendar Entry System ");
System.out.println( " " ) ;
String CustomersName = " ";
System.out.print( "Please enter your name ==> ") ;
CustomersName = inText.nextLine(); //Inputing Customers Name
System.out.println( " " ) ;
Date thisDate= new Date();
SimpleDateFormat dateForm = new SimpleDateFormat ("dd/MM/Y");
String startDate = dateForm.format(thisDate);
System.out.println(" Todays Date " +(dateForm.format(thisDate)));
System.out.println( " " ) ;
// To take the input
Scanner scanner = new Scanner(System.in);
System.out.println("Enter Cycle start date ");
String date = scanner.next();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/Y");
Date date2=null;
System.out.println( " " ) ;
Scanner scanner2 = new Scanner(System.in);
System.out.println("Enter Cycle end date ");
String date3 = scanner.next();
SimpleDateFormat date3Format = new SimpleDateFormat("dd/MM/Y");
Date date4=null;
System.out.println("\n\n--- Female Calender Results ---"); //Recipt for pizza order with name number of topings and overal price
System.out.println( " " ) ;
System.out.println("Client Name: " + CustomersName);
System.out.println("Lenght of cycle:") ; // this should have the number of days the cycle lasted
System.out.println("Aproximently next cycle: " + (" ")); // this should have 28 days end date of cycles
System.out.println(" ");
}
}
专注分享java语言的经验与见解,让所有开发者获益!
评论