可以直接通過jdk基本方法,獲取到當(dāng)前的時間
成都創(chuàng)新互聯(lián)從2013年創(chuàng)立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目做網(wǎng)站、網(wǎng)站制作網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元霸州做網(wǎng)站,已為上家服務(wù),為霸州各地企業(yè)和個人服務(wù),聯(lián)系電話:028-86922220
Date date= new Date();//創(chuàng)建一個時間對象,獲取到當(dāng)前的時間
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//設(shè)置時間顯示格式
String str = sdf.format(date);//將當(dāng)前時間格式化為需要的類型
System.out.println(str);//輸出結(jié)果
結(jié)果為:2015-11-06 13:53:54(實(shí)時)。
java獲取一個時間的年月日代碼及相關(guān)解釋說明參考下面代碼
package?zhidao;
import?java.util.Calendar;
public?class?Test?{
public?static?void?main(String[]?args)?{
Calendar?cal=Calendar.getInstance();//使用日歷類
int?year=cal.get(Calendar.YEAR);//獲取年份
int?month=cal.get(Calendar.MONTH)+1;//獲取月份,因?yàn)閺?開始的,所以要加1
int?day=cal.get(Calendar.DAY_OF_MONTH);//獲取天
System.out.println("結(jié)果:"+year+"-"+month+"-"+day);
}
}
import java.io.*;
public class Date{
public static void main(String [] args)throws NumberFormatException, IOException{
int year = 0;
int month = 0;
int day = 0;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
year = Integer.parseInt(br.readLine());
month = Integer.parseInt(br.readLine());
day = Integer.parseInt(br.readLine());
System.out.println(year + "年" + month + "月" + day + "日");
}
}
說明 :
輸入2008 回車
輸入 5 回車
輸入 30 回車
輸出 2008年5月30日
將這段代碼拷貝到UseBirthday類中和你的比較一下看看
public class UseBirthday {
public static void main(String[] args) {
Birthday a=new Birthday(2000,1,1);
a.GetBirthday();
a.Birthday(2008,5,12);
a.GetBirthday();
}
}
class Birthday {
private int Year, Month, Day;
Birthday(){
Year=2000;
Month=1;
Day=1;
}
Birthday(int y,int m,int d) {
Year = y;
Month = m;
Day = d;
}
public void Birthday(int y,int m,int d){
Year = y;
Month = m;
Day = d;
}
int Gety() {
return Year;
}
int Getm() {
return Month;
}
int Getd() {
return Day;
}
void GetBirthday() {
System.out.println(Gety() + "年" + Getm() + "月" + Getd() + "日");
}
}
寫了個簡明的,
import java.util.Calendar;
import java.util.Scanner;
public class Test {
static public void main(String 參數(shù)[]){
Calendar c = Calendar.getInstance();
Scanner sc = new Scanner(System.in);
System.out.println("請輸入年份:");
int year= sc.nextInt();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, Calendar.JANUARY);
c.set(Calendar.DAY_OF_MONTH, 1);
while(c.get(Calendar.YEAR)==year){
int wday=c.get(Calendar.DAY_OF_WEEK);
int mday=c.get(Calendar.DAY_OF_MONTH);
if(mday==1){
System.out.println("\n日\t一\t二\t三\t四\t五\t六\t第"+(c.get(Calendar.MONTH)+1)+"月");
System.out.println("---------------------------------------------------");
for(int i=0;iwday-1;i++) System.out.print(" \t");
}
System.out.print(mday+"\t");
if(wday==7) System.out.println();
c.add(Calendar.DAY_OF_YEAR, 1);
}
}
}
=======
請輸入年份:
2012
日 一 二 三 四 五 六 第1月
---------------------------------------------------
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
日 一 二 三 四 五 六 第2月
---------------------------------------------------
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29
日 一 二 三 四 五 六 第3月
---------------------------------------------------
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
日 一 二 三 四 五 六 第4月
---------------------------------------------------
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
日 一 二 三 四 五 六 第5月
---------------------------------------------------
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
日 一 二 三 四 五 六 第6月
---------------------------------------------------
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
日 一 二 三 四 五 六 第7月
---------------------------------------------------
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
日 一 二 三 四 五 六 第8月
---------------------------------------------------
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
日 一 二 三 四 五 六 第9月
---------------------------------------------------
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30
日 一 二 三 四 五 六 第10月
---------------------------------------------------
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
日 一 二 三 四 五 六 第11月
---------------------------------------------------
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30
日 一 二 三 四 五 六 第12月
---------------------------------------------------
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
按照你的要求編寫的Java日歷驗(yàn)證程序如下
UI.java
import?java.util.Scanner;
public?class?UI?{
static?Scanner?sc=new?Scanner(System.in);
public?static?int?askInt(String?s){
System.out.print(s);
return?sc.nextInt();
}
public?static?void?println(String?s){
System.out.println(s);
}
}
EE.java
public?class?EE?{
public?void?validateDateCore(){
int?year?=UI.askInt("Enter?the?year:?");
int?month=UI.askInt("Enter?the?month:?");
int?day=UI.askInt("Enter?the?day:?");
if(year??1){
UI.println("The?year?is?not?a?valid?number.");
return;
}
if(month1?||?month12){
UI.println("The?month?is?not?a?valid?number.");
return;
}
int?monthDay=0;
switch(month){
case?1:
case?3:
case?5:
case?7:
case?8:
case?10:
case?12:monthDay=31;break;
case?4:
case?6:
case?9:
case?11:monthDay=30;break;
case?2:
if((year%4==0??year%100!=0)?||?year%400==0){
monthDay=29;
}else{
monthDay=28;
}
break;
}
if(day1?||?daymonthDay){
UI.println("The?day?is?not?a?valid?number.");
return;
}else{
UI.println("It?is?"+day+"/"+month+"/"+year+".");
}
}
public?static?void?main(String[]?args)?{
new?EE().validateDateCore();
}
}
運(yùn)行結(jié)果
網(wǎng)頁名稱:JAVA中用代碼寫年月日,年月日代碼怎么寫
分享鏈接:http://chinadenli.net/article26/dsicjjg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營銷推廣、關(guān)鍵詞優(yōu)化、服務(wù)器托管、手機(jī)網(wǎng)站建設(shè)、域名注冊、虛擬主機(jī)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)