「java生日计算」java计算生日是星期几白盒测试
今天给各位分享java生日计算的知识,其中也会对java计算生日是星期几白盒测试进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、用Java代码求距离生日还有多少天?
- 2、java编写一个简单的输入生日计算下一个生日时间的代码?
- 3、java 这个是输入生日然后算出 从生日到现在 活了多少天 求代码
- 4、(用JAVA实现)请使用日期相关的api,控制台输入出生日期,计算出一个人出生了多少年?
用Java代码求距离生日还有多少天?
package com.example.time.test;
import com.example.time.Utils.DateUtil;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
/*
*@description:
*@author jiafeng
*@date 2019/12/6 0006 14:28
*/
public class brithday {
public static final String DATE_FORMAT = "yyyy-MM-dd";
public static void main(String[] args) {
String brithday = "12-07";
System.out.println(getBrithday(brithday));
}
public static int getBrithday(String brithday){
String dataOne = String.valueOf(getNowYear())+"-"+brithday;
String dataTwo = String.valueOf(getNowYear()+1)+"-"+brithday;
Date birthdayOne = DateUtil.parse(dataOne,DATE_FORMAT);
Date birthdayTwo = DateUtil.parse(dataTwo,DATE_FORMAT);
int n = 0;
if (birthdayOne.before(new Date())){
n = getIntervalDay(new Date(),birthdayTwo);
}else {
n = getIntervalDay(new Date(),birthdayOne);
}
return n;
}
/**
* 获取今年是哪一年
* @return
*/
public static Integer getNowYear(){
Date date = new Date();
GregorianCalendar gc=(GregorianCalendar) Calendar.getInstance();
gc.setTime(date);
return Integer.valueOf(gc.get(1));
}
/**
* 获取两个日期之间间隔的天数
* @author sunyy
* @return
*/
public static int getIntervalDay(Date start_date, Date end_date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
start_date = sdf.parse(sdf.format(start_date));
end_date = sdf.parse(sdf.format(end_date));
} catch (ParseException e) {
e.printStackTrace();
}
Calendar cal = Calendar.getInstance();
cal.setTime(start_date);
long time1 = cal.getTimeInMillis();
cal.setTime(end_date);
long time2 = cal.getTimeInMillis();
long between_days = (time2-time1)/(1000*3600*24);
return Integer.parseInt(String.valueOf(between_days));
}
}
java编写一个简单的输入生日计算下一个生日时间的代码?
import java.util.Calendar;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
/**
* Title: Test03.javabr
* Description:
*
* @author 王凯芳
* @date 2020年3月5日 下午6:03:04
* @version 1.0
*
* @request 编写一个方法能计算任何一个人今天离他最近下一次生日还有多少天,然后在主方法(main方法)中输入你的出生年月日,调用该方法的计算结果并输出信息“某某同学离自己最近下一次生日x天”。
*/
public class Test03 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你的姓名:");
String name = sc.nextLine();
System.out.println("请输入你的生日,格式为(2000/01/01):");
String line = sc.nextLine();
String[] strs = line.split("/");
if (strs.length == 3) {
int days = getDays(strs[0], strs[1], strs[2]);
if (days == 0) {
System.out.println(String.format("%s 同学,今天是你的生日,祝你生日快乐(#^.^#)", name, days));
} else {
System.out.println(String.format("%s 同学离自己最近下一次生日%d天。", name, days));
}
} else {
System.out.println("生日输入不正确!请按格式输入。");
}
sc.close();
}
/**
* 获取最近一次生日天数
*
* @param year
* @param month
* @param day
* @return
*/
public static int getDays(String year, String month, String day) {
Calendar now = Calendar.getInstance();
now.set(Calendar.HOUR_OF_DAY, 0);
now.set(Calendar.MINUTE, 0);
now.set(Calendar.SECOND, 0);
now.set(Calendar.MILLISECOND, 0);
int now_year = now.get(Calendar.YEAR);
Calendar birthday = Calendar.getInstance();
birthday.set(Calendar.YEAR, now_year);
birthday.set(Calendar.MONTH, Integer.parseInt(month) - 1);
birthday.set(Calendar.DAY_OF_MONTH, Integer.parseInt(day));
birthday.set(Calendar.HOUR_OF_DAY, 0);
birthday.set(Calendar.MINUTE, 0);
birthday.set(Calendar.SECOND, 0);
birthday.set(Calendar.MILLISECOND, 0);
long diff = now.getTimeInMillis() - birthday.getTimeInMillis();
if (diff == 0) {
return 0;
} else if (diff 0) {
long diffDays = TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS);
return Math.abs((int) diffDays);
} else {
birthday.add(Calendar.YEAR, 1);
long diffMi = birthday.getTimeInMillis() - now.getTimeInMillis();
long diffDays = TimeUnit.DAYS.convert(diffMi, TimeUnit.MILLISECONDS);
return (int) diffDays;
}
}
}
java 这个是输入生日然后算出 从生日到现在 活了多少天 求代码
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
/*
* 控制台输入生日,计算到今天为止进过了多少天
* 输入生日的格式:yyyy-MM-dd
*/
public class WorkDemo {
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Scanner sc = new Scanner(System.in);
System.out.print("请输入你的生日, 输入格式为:yyyy-MM-dd");
String birthday = sc.nextLine();
Date birDate = sdf.parse(birthday);
Date now = new Date();
long time = now .getTime() - birDate.getTime();
long day = time/1000/60/60/24;
System.out.println("到今天经历了:"+day+"天");
}
}
(用JAVA实现)请使用日期相关的api,控制台输入出生日期,计算出一个人出生了多少年?
这个问题主要涉及日期的解析及时间分量的计算。
思路:使用SimpleDateFormat将输入的字符串表示的日期解析为Date,再将Data转为Calendar,获取日期分类年份,然后与当前年份做差运算即可。
代码如下:
代码实现
java生日计算的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java计算生日是星期几白盒测试、java生日计算的信息别忘了在本站进行查找喔。