java判断开始时间结束时间
发布网友
发布时间:2022-04-25 08:39
我来回答
共7个回答
热心网友
时间:2022-04-25 10:09
try{
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM");
java.util.Calendar starCalendar = java.util.Calendar.getInstance();
java.util.Calendar endCalendar = java.util.Calendar.getInstance();
String starStr = "2011-11";
String endStr = "2012-02";
java.util.Date starDate = sdf.parse(starStr);
java.util.Date endDate = sdf.parse(endStr);
starCalendar.setTime(starDate);
endCalendar.setTime(endDate);
int instance = endCalendar.compareTo(starCalendar);
while(endCalendar.compareTo(starCalendar)>=0){
System.out.println(sdf.format(starCalendar.getTime()));
//这里我输出了,你可以自行处理,List等进行操作
starCalendar.add(Calendar.MONTH, 1);
}
}catch(ParseException e){
e.printStackTrace();
}
热心网友
时间:2022-04-25 11:27
按照你说的,做了一个案列。你看看是不是这样的
package com.zwd.first;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
public class AboutTime1 {
public static void main(String[] args) {
//开始时间
String startDate="2011-11";
//结束时间
String endDate="2012-02";
//取出开始的年份
int startYear=Integer.parseInt(startDate.substring(0, 4));
//取出开始的月份
int startMonth=Integer.parseInt(startDate.substring(5, 7));
//取出结束的年份
int endYear=Integer.parseInt(endDate.substring(0, 4));
//取出结束的月份
int endMonth=Integer.parseInt(endDate.substring(5, 7));
//计算出相差多少年
int rangeYear=endYear-startYear;
//计算相差多少月
int rangeMonth=0;
if(rangeYear>0){
rangeMonth=rangeYear*12-startMonth+endMonth;
}else {
rangeMonth=startMonth-endMonth;
}
SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM");
Date sdate=null;
try {
sdate = sf.parse(startDate);
} catch (ParseException e) {
e.printStackTrace();
}
//创建集合
List<String> list= new ArrayList<String>();
for(int i=0;i<=rangeMonth;i++){
Calendar calendar = Calendar.getInstance();
calendar.setTime(sdate);
calendar.add(Calendar.MONTH,+i);
String date = new SimpleDateFormat("yyyy-MM").format(calendar.getTime());
list.add(date);
}
//测试
for(String s:list){
System.out.println(s);
}
}
}
热心网友
时间:2022-04-25 13:01
public static void main(String[] args) {
try{
String startDateStr = "2013-8";
Integer startYear = Integer.parseInt(startDateStr.substring(0, 4));
Integer strartIndex = startDateStr.indexOf("-");
Integer strartMon = Integer.parseInt(startDateStr.substring(strartIndex + 1));
String endDateStr = "2018-5";
Integer endYear = Integer.parseInt(endDateStr.substring(0, 4));
Integer endIndex = endDateStr.indexOf("-");
Integer endMon = Integer.parseInt(endDateStr.substring(endIndex + 1));
if(strartIndex != 4 || endIndex != 4 || strartMon <= 0 || strartMon > 12 || endMon <= 0 || endMon > 12){
System.out.println("日期输入有误!");
return;
}
int forIndex = (endYear - startYear) * 12 + (endMon - strartMon) + 1;
if(forIndex > 0){
String[] dateAry = new String[forIndex];
Integer currentYear = startYear;
Integer currentMon = strartMon;
for(int i = 0; i < forIndex; i++){
if(currentMon > 12){
currentMon -= 12;
currentYear += 1;
}
dateAry[i] = currentYear + "-" + currentMon;
currentMon += 1;
}
for(int i = 0; i < dateAry.length; i++){
System.out.println(dateAry[i] + "\n");
}
}else{
System.out.println("开始日期必须小于结束日期!");
}
}catch(Exception e){
e.printStackTrace();
System.out.println("日期输入有误!");
}
}
热心网友
时间:2022-04-25 14:53
可以在前台用JQ判断!得到输入框的值,进行判断!
热心网友
时间:2022-04-25 17:01
用日历类 Calendar calendar = new GregorianCalendar();追问能否详细点!
追答楼下答得可以用。
热心网友
时间:2022-04-25 19:25
你可以得到具体的年月日,然后比较!
热心网友
时间:2022-04-25 22:07
什么意思?能说清楚点吗?追问补充问题↑
追答import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
public class myServlet {
public static void main(String[] args) throws Exception {
Date date = getDate("yyyy-MM", "2013-01");
List list = new ArrayList();
int i = getIndex(date, "2013-12");
for (int k = 0; k classza = obj.getClass();
String simple = "";
if ("String".equals(classza.getSimpleName())) {
simple = obj;
}
else {
return -1;
}
int year = Integer.parseInt(simple.substring(0, 4));
int month = Integer.parseInt(simple.substring(5, 7));
int index = (year - startDateYear) * 12 + (month - startDateMonth);
return index;
}
public static Date getDate(String style, String param) throws Exception {
return new SimpleDateFormat(style).parse(param);
}
public static String getDate(String style, Date date) {
return new SimpleDateFormat(style).format(date);
}
}