public class Birthday{
我們提供的服務(wù)有:成都網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)、微信公眾號(hào)開(kāi)發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、馬邊彝族ssl等。為數(shù)千家企事業(yè)單位解決了網(wǎng)站和推廣的問(wèn)題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的馬邊彝族網(wǎng)站制作公司
public static void main(String args[]){
System.out.println("凱旋,生日快樂(lè)");
}
}
獲取系統(tǒng)時(shí)間:
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);//小時(shí)
int second=ca.get(Calendar.SECOND);//秒
int WeekOfYear = ca.get(Calendar.DAY_OF_WEEK);
}
用系統(tǒng)時(shí)間跟生日比較就行了
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 編寫(xiě)一個(gè)方法能計(jì)算任何一個(gè)人今天離他最近下一次生日還有多少天,然后在主方法(main方法)中輸入你的出生年月日,調(diào)用該方法的計(jì)算結(jié)果并輸出信息“某某同學(xué)離自己最近下一次生日x天”。
*/
public class Test03 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("請(qǐng)輸入你的姓名:");
String name = sc.nextLine();
System.out.println("請(qǐng)輸入你的生日,格式為(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 同學(xué),今天是你的生日,祝你生日快樂(lè)(#^.^#)", name, days));
} else {
System.out.println(String.format("%s 同學(xué)離自己最近下一次生日%d天。", name, days));
}
} else {
System.out.println("生日輸入不正確!請(qǐng)按格式輸入。");
}
sc.close();
}
/**
* 獲取最近一次生日天數(shù)
*
* @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;
}
}
}
當(dāng)前文章:用java生日快樂(lè)代碼 Java生日快樂(lè)代碼
網(wǎng)頁(yè)地址:http://www.rwnh.cn/article2/higoic.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、全網(wǎng)營(yíng)銷推廣、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站營(yíng)銷、做網(wǎng)站、手機(jī)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)