⑴ 如何用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();