因?yàn)镴DK的月份表示范圍是0~11,分別表示1月至12月。
讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來(lái)自于我們對(duì)這個(gè)行業(yè)的熱愛。我們立志把好的技術(shù)通過(guò)有效、簡(jiǎn)單的方式提供給客戶,將通過(guò)不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:申請(qǐng)域名、虛擬空間、營(yíng)銷軟件、網(wǎng)站建設(shè)、蔚縣網(wǎng)站維護(hù)、網(wǎng)站推廣。
而用戶習(xí)慣表示的月份往往是從1開始至12之間。
為了將用戶習(xí)慣的月份表示法轉(zhuǎn)換到JDK固定的表示法,故將用戶表示的月份數(shù)減1,以得到JDK表示的月份數(shù)。
import java.util.Date;
import java.util.Calendar;
import java.text.SimpleDateFormat;
import java.util.*;
public class CalendarExample {
private static void CalendarTimemethod() {
Date date = Calendar.getInstance().getTime();
System.out.println("Current date and time is: " + date);
System.out.println();
}
private static void SimpleDateFormatmethod() {
Calendar date = Calendar.getInstance();
SimpleDateFormat dateformatter = new SimpleDateFormat(
"E yyyy.MM.dd 'at' hh:mm:ss a zzz");
System.out.println("Current date and time in simple date format: "
+ dateformatter.format(date.getTime()));
System.out.println();
}
private static void Adddates() {
System.out.println("Performing operations on calendar dates.");
// Get today's date
Calendar date = Calendar.getInstance();
Calendar cldr;
SimpleDateFormat dateformatter = new SimpleDateFormat(
"E yyyy.MM.dd 'at' hh:mm:ss a zzz");
cldr = (Calendar) date.clone();
cldr.add(Calendar.DAY_OF_YEAR, -(365 * 2));
System.out.println("Before two years it was: "
+ dateformatter.format(cldr.getTime()));
cldr = (Calendar) date.clone();
cldr.add(Calendar.DAY_OF_YEAR, +5);
System.out.println("After five years it will be: "
+ dateformatter.format(cldr.getTime()));
System.out.println();
}
private static void DateDifference() {
System.out.println("Difference between two dates");
Date startDate1 = new GregorianCalendar(2005, 02,
25, 14, 00).getTime();
Date endDate1 = new Date();
;
long diff = endDate1.getTime() - startDate1.getTime();
System.out.println(" Difference between " + endDate1);
System.out.println(" and " + startDate1 + " is " + (diff /
(1000L * 60L * 60L * 24L)) + " days.");
System.out.println();
}
private static void Getcalendermethods() {
System.out.println("Various get methods of the calendar class:");
Calendar calender = Calendar.getInstance();
System.out.println("Year : "
+ calender.get(Calendar.YEAR));
System.out.println("Month : "
+ calender.get(Calendar.MONTH));
System.out.println("Day of Month : "
+ calender.get(Calendar.DAY_OF_MONTH));
System.out.println("Day of Week : "
+ calender.get(Calendar.DAY_OF_WEEK));
System.out.println("Day of Year : "
+ calender.get(Calendar.DAY_OF_YEAR));
System.out.println("Week of Year : "
+ calender.get(Calendar.WEEK_OF_YEAR));
System.out.println("Week of Month : "
+ calender.get(Calendar.WEEK_OF_MONTH));
System.out.println("Day of the Week in Month : "
+ calender.get(Calendar.DAY_OF_WEEK_IN_MONTH));
System.out.println("Hour : " + calender.get(Calendar.HOUR));
System.out.println("AM PM : " + calender.get(Calendar.AM_PM));
System.out.println("Hour of the Day : "
+ calender.get(Calendar.HOUR_OF_DAY));
System.out.println("Minute : " + calender.get(Calendar.MINUTE));
System.out.println("Second : " + calender.get(Calendar.SECOND));
System.out.println();
}
public static void main(String[] args) {
System.out.println();
CalendarTimemethod();
SimpleDateFormatmethod();
Adddates();
DateDifference();
Getcalendermethods();
}
}
以上答案有林氏120燙傷中心提供。稍微再修改下就可以
String startDateStr = "";
String endDateStr = "";
TimeZone tzES2 = TimeZone.getTimeZone("GMT+8");
Calendar calES2 = Calendar.getInstance(tzES2);
Calendar ca = Calendar.getInstance();
Date now = ca.getTime();
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
dateFormat.setCalendar(calES2);
ca.set(Calendar.DATE, ca.getActualMinimum(Calendar.DAY_OF_MONTH));//設(shè)置當(dāng)前日期為當(dāng)前月的第一天
now = ca.getTime();
startDateStr = dateFormat.format(now);
ca.set(Calendar.DATE, ca.getActualMaximum(Calendar.DAY_OF_MONTH));//設(shè)置當(dāng)前日期為當(dāng)前月的最后一天
now = ca.getTime();
endDateStr = dateFormat.format(now);
System.out.println(startDateStr);
System.out.println(endDateStr);
ca.add(Calendar.MONTH, -1);//設(shè)置日期為當(dāng)前日期加上-1個(gè)月(即減去1個(gè)月)的日期。
后面的操作同上!
抽了點(diǎn)時(shí)間寫了些代碼:給你看看是不是你想要的,你是初學(xué)者把,俺給你加些注釋 package yjh.action; import java.text.SimpleDateFormat; //定制日期格式的類 import java.util.Calendar; //日歷類 import java.util.Date; //當(dāng)前日期和時(shí)間的Data類 import java.util.Scanner;//讀取用戶輸入的類 public class DataCounter{ public int getDay(String strDate) throws Exception { int day = 0; //設(shè)定一個(gè)特定的日期格式,例如2009-01-03 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); //把用戶輸入的字符串轉(zhuǎn)化為日期和時(shí)間的格式 Date date = format.parse(strDate); //返回系統(tǒng)的當(dāng)前狀態(tài)時(shí)間 Calendar cal = Calendar.getInstance(); //用給定的 date 設(shè)置此 cal cal.setTime(date); //返回年月日中的天數(shù) day = cal.get(cal.DAY_OF_YEAR); return day; } public static void main(String args[]) { try { System.out.println("請(qǐng)輸入日期,格式如:2009-03-25:"); //讀取用戶的輸入 Scanner scan = new Scanner(System.in); String str = scan.next(); int day; day = new DataCounter().getDay(str); System.out.println(str + "是" + str.subSequence(0, 4) + "年的第" + day + "天"); } catch (Exception e) { // TODO 自動(dòng)生成 catch 塊 e.printStackTrace(); } } }
public String getMonthName() {
int month = (int) (Math.random() * 12 + 1);
switch (month) {
case 1:
return "一月";
case 2:
return "二月";
case 3:
return "三月";
case 4:
return "四月";
case 5:
return "五月";
case 6:
return "六月";
case 7:
return "七月";
case 8:
return "八月";
case 9:
return "九月";
case 10:
return "十月";
case 11:
return "十一月";
default:
return "十二月";
}
}
PS: 是不是作業(yè),上學(xué)要好好上呀,不能老指望別人,你學(xué)軟件學(xué)費(fèi)挺貴的吧!
Java是一門面向?qū)ο缶幊陶Z(yǔ)言,不僅吸收了C++語(yǔ)言的各種優(yōu)點(diǎn),還摒棄了C++里難以理解的多繼承、指針等概念,因此Java語(yǔ)言具有功能強(qiáng)大和簡(jiǎn)單易用兩個(gè)特征。Java語(yǔ)言作為靜態(tài)面向?qū)ο缶幊陶Z(yǔ)言的代表,極好地實(shí)現(xiàn)了面向?qū)ο罄碚摚试S程序員以優(yōu)雅的思維方式進(jìn)行復(fù)雜的編程。
Java具有簡(jiǎn)單性、面向?qū)ο?、分布式、健壯性、安全性、平臺(tái)獨(dú)立與可移植性、多線程、動(dòng)態(tài)性等特點(diǎn)。Java可以編寫桌面應(yīng)用程序、Web應(yīng)用程序、分布式系統(tǒng)和嵌入式系統(tǒng)應(yīng)用程序等。