java猜生日代码
java编写一个简单的输入生日计算下一个生日时间的代码?
import java.util.Calendar;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
/**
* Description:
*
* @author 王凯芳
* @version 1.0
* @request 编写一个方法能计算任何一个人今天离他最近下一次生日还有多少天,然后在主方法(main方法)中输入你的出生年月日,调用该方法的计算结果并输出信息"某某同学离自己最近下一次生日x天".
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你的姓名:");
String name = sc.nextLine();
String line = sc.nextLine();
String[] strs = line.split("/");
if (days == 0) {
System.out.println(String.format("%s 同学,今天是你的生日,祝你生日快乐(#^.^#)", name, days));
} else {
System.out.println(String.format("%s 同学离自己最近下一次生日%d天.", name, days));
}
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);
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.io.*;
import java.util.*;
public class GetBirth {
int year = 0;
int month = 0;
int day = 0;
Calendar cld = Calendar.getInstance();//创建一个日历
public GetBirth(){
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入年、月、日:");
try {
year = Integer.parseInt(in.readLine());
month = Integer.parseInt(in.readLine());
day = Integer.parseInt(in.readLine());
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (IOException e) {
//将日历时间设置成生日那天
public void setDate(int year,int month,int day){
cld.set(year,month-1,day);
//获取该日是一年当中的第几天
public int getDay(){
//获取该日是星期几
public String getDate(){
return getWeekday(date);
public String getWeekday(int dayofweek){
switch(dayofweek){
case 1: return "星期日";
default:return "error";
GetBirth gb = new GetBirth();
gb.setDate(gb.year, gb.month, gb.day);
System.out.println(gb.getDay());
System.out.println(gb.getDate());
希望对楼主有所帮助,谢谢.
JAVA猜生日的程序
不知道这个猜生日的程序具体难度如何,是否会使用很复杂的算法
在一般公司里面,一般不会用到复杂的算法的,需要的是对java基础的掌握情况和其他的框架的知识,所以你只需要掌握牢java基础就好了
以上就是茂凯百科网小编为大家整理的java猜生日代码相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!