public class Birthday{
我们提供的服务有:成都网站设计、网站建设、微信公众号开发、网站优化、网站认证、马边彝族ssl等。为数千家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的马边彝族网站制作公司
public static void main(String args[]){
System.out.println("凯旋,生日快乐");
}
}
获取系统时间:
import java.util.*;
import java.text.*;
public class TestDate {
public static void main(String[] args) {
Calendar ca = Calendar.getInstance();
int year = ca.get(Calendar.YEAR);//获取年份
int month=ca.get(Calendar.MONTH);//获取月份
int day=ca.get(Calendar.DATE);//获取日
int minute=ca.get(Calendar.MINUTE);//分
int hour=ca.get(Calendar.HOUR);//小时
int second=ca.get(Calendar.SECOND);//秒
int WeekOfYear = ca.get(Calendar.DAY_OF_WEEK);
}
用系统时间跟生日比较就行了
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;
}
}
}