⑴ 如何用java去操作「日期」函數,取得月初和月末
可以使用.util.Calendar來取得相關日期信息,下面給個示例供參考:
Java代碼:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class DateDemo {
static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
public static void main(String[] args) throws ParseException {
String date = "2011-04-25 22:28:30";
System.out.println(date);
System.out.println("年份:" + getYear(date));
System.out.println("月份:" + getMonth(date));
System.out.println("日期:" + getDay(date));
System.out.println("月初日期是: " + getMinMonthDate(date));
System.out.println("月末日期是: " + getMaxMonthDate(date));
}
/**
* 獲取日期年份
* @param date
* @return
* @throws ParseException
*/
public static int getYear(String date) throws ParseException{
Calendar calendar = Calendar.getInstance();
calendar.setTime(dateFormat.parse(date));
return calendar.get(Calendar.YEAR);
}
/**
* 獲取日期月份
* @param date
* @return
* @throws ParseException
*/
public static int getMonth(String date) throws ParseException{
Calendar calendar = Calendar.getInstance();
calendar.setTime(dateFormat.parse(date));
return (calendar.get(Calendar.MONTH) + 1);
}
/**
* 獲取日期號
* @param date
* @return
* @throws ParseException
*/
public static int getDay(String date) throws ParseException{
Calendar calendar = Calendar.getInstance();
calendar.setTime(dateFormat.parse(date));
return calendar.get(Calendar.DAY_OF_MONTH);
}
/**
* 獲取月份起始日期
* @param date
* @return
* @throws ParseException
*/
public static String getMinMonthDate(String date) throws ParseException{
Calendar calendar = Calendar.getInstance();
calendar.setTime(dateFormat.parse(date));
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
return dateFormat.format(calendar.getTime());
}
/**
* 獲取月份最後日期
* @param date
* @return
* @throws ParseException
*/
public static String getMaxMonthDate(String date) throws ParseException{
Calendar calendar = Calendar.getInstance();
calendar.setTime(dateFormat.parse(date));
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
return dateFormat.format(calendar.getTime());
}
}
⑵ java怎麼獲取當前月的第一天和最後一天
import java.util.Calendar;
import java.util.Date;
public class Test {
public static void main(String args[]) {
System.out.println(getLastDayOfMonth(new Date()));
}
public static Date getLastDayOfMonth(Date sDate1) {
Calendar cDay1 = Calendar.getInstance();
cDay1.setTime(sDate1);
final int lastDay = cDay1.getActualMaximum(Calendar.DAY_OF_MONTH);
Date lastDate = cDay1.getTime();
lastDate.setDate(lastDay);
return lastDate;
}
}
⑶ 在java中如何通過一個月份,獲得該月份的第一天的日期
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
public class Test {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance(Locale.CHINA);
calendar.setTime(new Date());// 設置當前日期
int day = calendar.getMinimum(Calendar.DAY_OF_MONTH);// 取得當前月的最小日期(天)
calendar.set(Calendar.DAY_OF_MONTH, day);// 設置天回
String sDate = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());// 格式化成字元串答
System.out.println(sDate);// 列印
}
}
⑷ java 根據日期獲取當月所有日期,和 上個月所有日期
importjava.text.SimpleDateFormat;
importjava.util.Calendar;
importjava.util.Date;
publicclassMain{
=newSimpleDateFormat("yyyy-MM-dd");
publicstaticvoidmain(Stringargs[]){
=newDate();
//月初
System.out.println("月初"+sdf.format(getMonthStart(d)));
//月末
System.out.println("月末"+sdf.format(getMonthEnd(d)));
Datedate=getMonthStart(d);
DatemonthEnd=getMonthEnd(d);
while(!date.after(monthEnd)){
System.out.println(sdf.format(date));
date=getNext(date);
}
}
(Datedate){
Calendarcalendar=Calendar.getInstance();
calendar.setTime(date);
intindex=calendar.get(Calendar.DAY_OF_MONTH);
calendar.add(Calendar.DATE,(1-index));
returncalendar.getTime();
}
privatestaticDategetMonthEnd(Datedate){
Calendarcalendar=Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.MONTH,1);
intindex=calendar.get(Calendar.DAY_OF_MONTH);
calendar.add(Calendar.DATE,(-index));
returncalendar.getTime();
}
privatestaticDategetNext(Datedate){
Calendarcalendar=Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DATE,1);
returncalendar.getTime();
}
}
這是一個月的 你改改就能寫出上個月的了吧
⑸ java Date類型 按年,月,日 取出並輸出,怎麼編寫
這是根據你的要求編寫的代碼。
import java.util.Calendar;
import java.sql.Date;
public class TestAA {
/**
* @param args
*/
public static void main(String[] args) {
Calendar cld = Calendar.getInstance();
Date date = new java.sql.Date(1319534374312l);;
cld.setTime(date);
/**
* 註:在jdk1.6以後下列方法都已過時
* date.getYear();
* date.getMonth();
* date.getDay();
*/
System.out.println("日期為:"+date.toString());
//方法一
System.out.println("年份:"+cld.get(Calendar.YEAR));
System.out.println("月份:"+(cld.get(Calendar.MONTH)+1));
System.out.println("日:"+cld.get(Calendar.DAY_OF_MONTH));
//方法er
System.out.println("年份:"+date.toString().split("-")[0]);
System.out.println("月份:"+date.toString().split("-")[1]);
System.out.println("日:"+date.toString().split("-")[2]);
}
}
你看看,應該沒問題。
⑹ java date怎樣賦值
java 中date賦值方法:
1.通過new對象獲取當前的時間復制給date;
Datedate=newDate();
2.通過SimpleDateFormat將字元串轉換為date:
SimpleDateFormatformat=newSimpleDateFormat("yyyy年版MM月ddHH:mm:ss");
Datedate=format.parse(str);//str表示yyyy年MM月ddHH:mm:ss格式字元串
3.通過Calendar獲得權date
Calendarcalendar=Calendar.getInstance();
Datedate=calendar.getTime();
⑺ java怎麼得到某個月的第一天和最後一天的日期,以2012-06為例
今天剛好寫了這個代碼
Calendar cal = Calendar.getInstance();
// 不加下面2行,就是取當前時間前一個月的回第一天答及最後一天
cal.set(Calendar.YEAR,2012)
cal.set(Calendar.MONTH, 6);
cal.set(Calendar.DAY_OF_MONTH, 1);
cal.add(Calendar.DAY_OF_MONTH, -1);
Date lastDate = cal.getTime();
cal.set(Calendar.DAY_OF_MONTH, 1);
Date firstDate = cal.getTime();