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();
}
}
这是一个月的 你改改就能写出上个月的了吧
package util;
import javamathBigDecimal;
import javatextParseException;
import javatextSimpleDateFormat;
import javautilCalendar;
import javautilDate;
/
获取系统时间
/
public class DateUtil {
/ 日志对象 /
// private static Logger logger = LoggergetLogger(SystemUtilclass);
/ 获取年份 /
public static final int YEAR = 1;
/ 获取年月 /
public static final int YEARMONTH = 2;
/ 获取年月日 /
public static final int YEARMONTHDAY = 3;
/ 获取年月日,小时 /
public static final int YMD_HOUR = 4;
/ 获取年月日,小时,分钟 /
public static final int YMD_HOURMINUTE = 5;
/ 获取年月日,时分秒 /
public static final int FULL = 6;
/ 获取年月日时分秒 格式:yyyyMMddHHmmss /
public static final int UTILTIME = 7;
/
根据指定时间格式类型得到当前时间
@param type
时间类型
@return String 字符串时间
/
public static synchronized String getCurrentTime(int type) {
String format = getFormat(type);
SimpleDateFormat timeformat = new SimpleDateFormat(format);
Date date = new Date();
return timeformatformat(date);
}
/
返回当前系统时间的年月日
@return
/
public static synchronized String getCurrentTime() {
SimpleDateFormat timeformat = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
return timeformatformat(date);
}
/
根据参数格式,格式化当前日期
@param format
@return
/
public static synchronized String getDateString(String format) {
SimpleDateFormat timeformat = new SimpleDateFormat(format);
Date date = new Date();
return timeformatformat(date);
}
/
根据指定时间格式类型,格式化时间格式
@param type
时间格式类型
@return
/
private static String getFormat(int type) {
String format = "";
if (type == 1) {
format = "yyyy";
} else if (type == 2) {
format = "yyyy-MM";
} else if (type == 3) {
format = "yyyy-MM-dd";
} else if (type == 4) {
format = "yyyy-MM-dd HH";
} else if (type == 5) {
format = "yyyy-MM-dd HH:mm";
} else if (type == 6) {
format = "yyyy-MM-dd HH:mm:ss";
} else if (type == 7) {
format = "yyyyMMddHHmmss";
} else {
throw new RuntimeException("日期格式参数错误");
}
return format;
}
public static int getYear(String dateString) {
SimpleDateFormat dd = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = ddparse(dateString);
Calendar cal = CalendargetInstance();
calsetTime(date);
return calget(CalendarYEAR);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static int getMonth(String dateString) {
SimpleDateFormat dd = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = ddparse(dateString);
Calendar cal = CalendargetInstance();
calsetTime(date);
return calget(CalendarMONTH)+1;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static int getDay(String dateString) {
SimpleDateFormat dd = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = ddparse(dateString);
Calendar cal = CalendargetInstance();
calsetTime(date);
return calget(CalendarDAY_OF_MONTH);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static Date StringToDate(String dateStr, String formatStr) {
SimpleDateFormat dd = new SimpleDateFormat(formatStr);
Date date = null;
try {
date = ddparse(dateStr);
} catch (ParseException e) {
eprintStackTrace();
}
return date;
}
/
当前日期和参数日期距离的小时数 日期格式:yyyy-MM-dd HH:mm:ss
@param date
@return
/
public static double getHours(String date) {
SimpleDateFormat timeformat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
try {
Date d = new Date();
Date d1 = timeformatparse(date);
long temp = dgetTime() - d1getTime();
double f = temp / 3600000d;
BigDecimal b = new BigDecimal(f);
double f1 = bsetScale(2, BigDecimalROUND_HALF_UP)doubleValue();
return f1;
} catch (Exception e) {
eprintStackTrace();
throw new RuntimeException(e);
}
}
public static void main(String a[]) {
try {
int aa = getYear("2012-01-08");
Systemoutprintln(aa);
} catch (Exception e) {
eprintStackTrace();
}
}
}
那要看对方提供的是什么对象,如果是String类型的,那么就需要先转成Date对象,再转成Calendar对象,如果直接给的Date那么更简单,转成Calendar就可以了。
import javatextSimpleDateFormat;
import javautilCalendar;
import javautilDate;
public class Test12{
public static void main(String args[]) throws Exception{
String d1 = "2012-04-02";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdfparse(d1);
Calendar c = CalendargetInstance();
csetTime(date);
Systemoutprintln(cget(CalendarMONTH));
}
}
这个月份是从0开始到11的,所以4月会输出3
以上就是关于java 根据日期获取当月所有日期,和 上个月所有日期全部的内容,包括:java 根据日期获取当月所有日期,和 上个月所有日期、java 获取当前日期,应该如何 *** 作呢、java 如何解析年月日,比如别人给我传一个年月日,我想单独获取月份怎么解析谢谢高手!等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)