不太懂你什么意思,LocalDate 是類(lèi)型,只能說(shuō)最后用LocalDate 來(lái)接收,你可以接收一個(gè)生日字符串,然后通過(guò)LocalDate.parse("2022-09-23",DateTimeFormatter.ofPattern("yyyy-MM-dd")) 來(lái)轉(zhuǎn)換成LocalDate,或者直接接收3個(gè)int類(lèi)型變量,通過(guò)LocalDate.of(2022,9,23)來(lái)創(chuàng)建一個(gè)LocalDate日期。
創(chuàng)新互聯(lián)是一家以網(wǎng)絡(luò)技術(shù)公司,為中小企業(yè)提供網(wǎng)站維護(hù)、成都網(wǎng)站建設(shè)、做網(wǎng)站、網(wǎng)站備案、服務(wù)器租用、域名申請(qǐng)、軟件開(kāi)發(fā)、小程序開(kāi)發(fā)等企業(yè)互聯(lián)網(wǎng)相關(guān)業(yè)務(wù),是一家有著豐富的互聯(lián)網(wǎng)運(yùn)營(yíng)推廣經(jīng)驗(yàn)的科技公司,有著多年的網(wǎng)站建站經(jīng)驗(yàn),致力于幫助中小企業(yè)在互聯(lián)網(wǎng)讓打出自已的品牌和口碑,讓企業(yè)在互聯(lián)網(wǎng)上打開(kāi)一個(gè)面向全國(guó)乃至全球的業(yè)務(wù)窗口:建站歡迎咨詢(xún):13518219792
編寫(xiě)一個(gè)簡(jiǎn)單的生日快樂(lè)APP
一、關(guān)閉之前的helloworld程序
點(diǎn)擊file,然后close project,就完成關(guān)閉了。
二、創(chuàng)建一個(gè)新的happybirthday程序
三、下面開(kāi)始整個(gè)APP的修改
1、如何查看Androidstudio中activity_main.xml的源代碼,在圖中 灰色部分現(xiàn)在是Design,點(diǎn)到code就會(huì)出現(xiàn)activity_main_xml的代碼了。
2、進(jìn)行如下代碼的修改。把根布局改成相對(duì)布局 androidx.constraintlayout.widget.ConstraintLayout改為RelativeLayout
把多余的代碼刪除掉 ,并將文本內(nèi)容改為android:text="祝你生日快樂(lè)!"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent"
并可以修改文字的大小,顏色,添加邊框.。代碼如下:
3、在左側(cè)目錄下,res是存放資源文件的(圖片放進(jìn)來(lái));java是存放java代碼的;mainfests是存放配置文件的。因此將圖片復(fù)制在最側(cè)res目錄下。
4、將代碼補(bǔ)充完整
代碼:
?xml version="1.0" encoding="utf-8"?
RelativeLayout xmlns:android=""
xmlns:app=""
xmlns:tools=""
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
TextView
android:layout_margin="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="祝你生日快樂(lè)!"
android:textColor="@android:color/holo_orange_light"
android:textSize="22dp"
/
ImageView
android:layout_centerInParent="true"
android:src="@drawable/img"
android:layout_width="match_parent"
android:layout_height="wrap_content" /
/RelativeLayout
5、最后的效果如圖所示
6、可在手機(jī)中看到happybirthday的程序包,打開(kāi)便是上面的畫(huà)面。
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;
}
}
}
分享名稱(chēng):java生日輸入代碼 java中生日類(lèi)型用什么
網(wǎng)頁(yè)網(wǎng)址:http://jinyejixie.com/article28/dopcecp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)、企業(yè)建站、網(wǎng)頁(yè)設(shè)計(jì)公司、、商城網(wǎng)站、網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)