import javatextSimpleDateFormat;
import javautilCalendar;
import javautilDate;
public class Main {
private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
public static void main(String args[]) {
Date d = new Date();
// 月初
Systemoutprintln("月初" + sdfformat(getMonthStart(d)));
// 月末
Systemoutprintln("月末" + sdfformat(getMonthEnd(d)));
Date date = getMonthStart(d);
Date monthEnd = getMonthEnd(d);
while (!dateafter(monthEnd)) {
Systemoutprintln(sdfformat(date));
date = getNext(date);
}
}
private static Date getMonthStart(Date date) {
Calendar calendar = CalendargetInstance();
calendarsetTime(date);
int index = calendarget(CalendarDAY_OF_MONTH);
calendaradd(CalendarDATE, (1 - index));
return calendargetTime();
}
private static Date getMonthEnd(Date date) {
Calendar calendar = CalendargetInstance();
calendarsetTime(date);
calendaradd(CalendarMONTH, 1);
int index = calendarget(CalendarDAY_OF_MONTH);
calendaradd(CalendarDATE, (-index));
return calendargetTime();
}
private static Date getNext(Date date) {
Calendar calendar = CalendargetInstance();
calendarsetTime(date);
calendaradd(CalendarDATE, 1);
return calendargetTime();
}
}
这是一个月的 你改改就能写出上个月的了吧
可以使用javautilCalendar来取得相关日期信息,下面给个示例供参考:
Java代码:
import javatextParseException;
import javatextSimpleDateFormat;
import javautilCalendar;
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";
Systemoutprintln(date);
Systemoutprintln("年份:" + getYear(date));
Systemoutprintln("月份:" + getMonth(date));
Systemoutprintln("日期:" + getDay(date));
Systemoutprintln("月初日期是: " + getMinMonthDate(date));
Systemoutprintln("月末日期是: " + getMaxMonthDate(date));
}
/
获取日期年份
@param date
@return
@throws ParseException
/
public static int getYear(String date) throws ParseException{
Calendar calendar = CalendargetInstance();
calendarsetTime(dateFormatparse(date));
return calendarget(CalendarYEAR);
}
/
获取日期月份
@param date
@return
@throws ParseException
/
public static int getMonth(String date) throws ParseException{
Calendar calendar = CalendargetInstance();
calendarsetTime(dateFormatparse(date));
return (calendarget(CalendarMONTH) + 1);
}
/
获取日期号
@param date
@return
@throws ParseException
/
public static int getDay(String date) throws ParseException{
Calendar calendar = CalendargetInstance();
calendarsetTime(dateFormatparse(date));
return calendarget(CalendarDAY_OF_MONTH);
}
/
获取月份起始日期
@param date
@return
@throws ParseException
/
public static String getMinMonthDate(String date) throws ParseException{
Calendar calendar = CalendargetInstance();
calendarsetTime(dateFormatparse(date));
calendarset(CalendarDAY_OF_MONTH, calendargetActualMinimum(CalendarDAY_OF_MONTH));
return dateFormatformat(calendargetTime());
}
/
获取月份最后日期
@param date
@return
@throws ParseException
/
public static String getMaxMonthDate(String date) throws ParseException{
Calendar calendar = CalendargetInstance();
calendarsetTime(dateFormatparse(date));
calendarset(CalendarDAY_OF_MONTH, calendargetActualMaximum(CalendarDAY_OF_MONTH));
return dateFormatformat(calendargetTime());
}
}
Calendar ca = CalendargetInstance(); int year = caget(CalendarYEAR);//获取年份 int month=caget(CalendarMONTH);//获取月份 int day=caget(CalendarDATE);//获取日 int minute=caget(CalendarMINUTE);//分 int hour=caget(CalendarHOUR);//小时 int second=caget(CalendarSECOND);//秒 int WeekOfYear = caget(CalendarDAY_OF_WEEK);
获取的是阳历,补充一点,一般都是用阳历的
阳历也叫公历,来源于西方。比如算星座时、就是按照阳历(公历)计算的。
阴历也叫农历,来源于我们中国。
比如我们俗称的八月十五中秋节、清明节、七夕、春节、就是按照农历来计算的。而元旦、和西方的情人节(214)和圣诞节、国庆节、建军节、党的生日、都是按照公历来计算的。
他们的主要区别就在于阳历是国际通用的,而农历是我们中国特有的。
编程语言是国外的,怎么可能用来获取我们中国特有的时间呢
1java中关于获取时间的所有方式如下,
Date dNow = new Date(); //当前时间
Date dBefore = new Date();
Calendar calendar = CalendargetInstance(); //得到日历
calendarsetTime(dNow);//把当前时间赋给日历
calendaradd(CalendarDAY_OF_MONTH, -1); //设置为前一天
dBefore = calendargetTime(); //得到前一天的时间
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-ddHH:mm:ss");//设置时间格式
String defaultStartDate = sdfformat(dBefore); //格式化前一天
String defaultEndDate = sdfformat(dNow); //格式化当前时间
Systemoutprintln("前一天的时间是:" + defaultStartDate);
Systemoutprintln("生成的时间是:" + defaultEndDate);
2如上方式能得到当前时间的前一天时间,同理,可以得到前一天的前一天,那么,前几天的时间都可以这样获得,如果觉得方案还是不可行,可以使用第三方的日历组件来获得更加准确的全年三百六十五天的时间
另有:
Calendar 与 Date 的转换:
Calendar calendar = CalendargetInstance();
// 从一个 Calendar 对象中获取 Date 对象
Date date = calendargetTime();
// 将 Date 对象反应到一个 Calendar 对象中,
// 先获得一个实例,然后设置 Date 对象
calendarsetTime(date);
月份的起始值为0而不是1,所以要设置八月时,我们用7而不是8
calendarset(CalendarMONTH, 7);
计算2000-01-01是星期几
calendarget(calendarDAY_OF_WEEK )
3实例代码测试如下
Java代码import Javautil;
public class ShowDate {
public static void main(String[] args) {
Calendar calendar = new GregorianCalendar();
Date trialTime = new Date();
calendarsetTime(trialTime);
// print out a bunch of interesting things
Systemoutprintln("ERA: " + calendarget(CalendarERA));
Systemoutprintln("YEAR: " + calendarget(CalendarYEAR));
Systemoutprintln("MONTH: " + calendarget(CalendarMONTH));
Systemoutprintln("WEEK_OF_YEAR: " + calendarget(CalendarWEEK_OF_YEAR));
Systemoutprintln("WEEK_OF_MONTH: " + calendarget(CalendarWEEK_OF_MONTH));
Systemoutprintln("DATE: " + calendarget(CalendarDATE));
Systemoutprintln("DAY_OF_MONTH: " + calendarget(CalendarDAY_OF_MONTH));
Systemoutprintln("DAY_OF_YEAR: " + calendarget(CalendarDAY_OF_YEAR));
Systemoutprintln("DAY_OF_WEEK: " + calendarget(CalendarDAY_OF_WEEK));
Systemoutprintln("DAY_OF_WEEK_IN_MONTH: " + calendarget(CalendarDAY_OF_WEEK_IN_MONTH));
Systemoutprintln("AM_PM: " + calendarget(CalendarAM_PM));
Systemoutprintln("HOUR: " + calendarget(CalendarHOUR));
Systemoutprintln("HOUR_OF_DAY: " + calendarget(CalendarHOUR_OF_DAY));
Systemoutprintln("MINUTE: " + calendarget(CalendarMINUTE));
Systemoutprintln("SECOND: " + calendarget(CalendarSECOND));
Systemoutprintln("MILLISECOND: " + calendarget(CalendarMILLISECOND));
Systemoutprintln("ZONE_OFFSET: " + (calendarget(CalendarZONE_OFFSET)/(60601000)));
Systemoutprintln("DST_OFFSET: " + (calendarget(CalendarDST_OFFSET)/(60601000)));
Systemoutprintln("Current Time, with hour reset to 3");
calendarclear(CalendarHOUR_OF_DAY); // so doesn't override
calendarset(CalendarHOUR, 3);
Systemoutprintln("ERA: " + calendarget(CalendarERA));
Systemoutprintln("YEAR: " + calendarget(CalendarYEAR));
Systemoutprintln("MONTH: " + calendarget(CalendarMONTH));
Systemoutprintln("WEEK_OF_YEAR: " + calendarget(CalendarWEEK_OF_YEAR));
Systemoutprintln("WEEK_OF_MONTH: " + calendarget(CalendarWEEK_OF_MONTH));
Systemoutprintln("DATE: " + calendarget(CalendarDATE));
Systemoutprintln("DAY_OF_MONTH: " + calendarget(CalendarDAY_OF_MONTH));
Systemoutprintln("DAY_OF_YEAR: " + calendarget(CalendarDAY_OF_YEAR));
Systemoutprintln("DAY_OF_WEEK: " + calendarget(CalendarDAY_OF_WEEK));
Systemoutprintln("DAY_OF_WEEK_IN_MONTH: " + calendarget(CalendarDAY_OF_WEEK_IN_MONTH));
以上就是关于java 根据日期获取当月所有日期,和 上个月所有日期全部的内容,包括:java 根据日期获取当月所有日期,和 上个月所有日期、如何用java去 *** 作“日期”函数,取得月初和月末、java如何通过calendar类获取当前系统日期等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)