如何把字符串"20030315"和"20030404"转化为Date,并且得到然后把其间的日期在转化成String放入一个String[].

zhoulf 2003-04-04 10:54:19
例如:有字符20030227,20030302,得到20030227,20030228,20030301,20030302
...全文
14 点赞 收藏 10
写回复
10 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
datapub 2003-04-04
//Test.java
import java.util.Date;

class Test {
public static void main(String args[]) {
Date newDate = new Date("20030404"); //将字符串"20030404"转化为一个Date
system.out.println("The date is :" + newDate );
}
}
回复
AllError 2003-04-04
还有两个日期的前后判断...
回复
AllError 2003-04-04
package com.turnpage;

/**
* Created by IntelliJ IDEA.
* User: Administrator
* Date: 2003-4-4
* Time: 11:53:30
* To change this template use Options | File Templates.
*/
import java.util.Calendar;
import java.util.Vector;
import java.text.SimpleDateFormat;
public class test {

public static void main(String args[]){
Calendar scal = Calendar.getInstance();
Calendar cal = Calendar.getInstance();
Vector list=new Vector();
String str="";
int syear=2003,smonth=4,sday=9;
int eyear=2004,emonth=2,eday=7;
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
scal.set(syear,smonth,sday);
if((syear==eyear) && (smonth==emonth)){
//如果是同一月份
for(int i=sday;i<=(eday+1);i++){
cal.set(syear,smonth,i);
str=formatter.format(cal.getTime()) ;
list.addElement(str);
}
}else{
//不是同一月份
for(int m=smonth;m<((eyear-syear)*12+(emonth-smonth)+1);m++){
cal.set(syear,m,1);
//每月的天数
int maxday = scal.getActualMaximum(scal.DATE);
//if(头一个月){开始天数循环到月底}
//if(最后一个月(一号循环到最后一天))
for(int n=1;n<maxday+1;n++){
cal.set(syear,m,n);
str=formatter.format(cal.getTime()) ;
list.addElement(str);
}
}
}
//有了list,剩下的不用我说了吧



}
}
回复
AllError 2003-04-04
java.util.calendar
回复
zez 2003-04-04
用DateFormat类吧,试试看 :)
回复
zhoulf 2003-04-04
多谢各位了!
回复
pengji 2003-04-04
花了点时间写了一个,检查什么都加了!希望对你有用!

import java.text.*;
import java.util.*;
public class DateTest
{
public static Object[] getValue(String date1,String date2) throws Exception
{
SimpleDateFormat df =new SimpleDateFormat("yyyyMMdd");
df.setLenient(false);
Date dDate1;
Date dDate2;
try
{
dDate1 = df.parse(date1);
dDate2 = df.parse(date2);
}catch(Exception e)
{
throw new Exception("input args format error!");
}
if (dDate1.compareTo(dDate2)>0)
{
throw new Exception("date1 > date2");
}
int yyyy1 = Integer.parseInt(date1.substring(0,4));
int yyyy2 = Integer.parseInt(date2.substring(0,4));
int mm1 = Integer.parseInt(date1.substring(4,6));
int mm2 = Integer.parseInt(date2.substring(4,6));
int day1 = Integer.parseInt(date1.substring(6,8));
int day2 = Integer.parseInt(date2.substring(6,8));

List list = new LinkedList();
list.add(date1);
String tempDate="";

//add some element still the year same
while(yyyy1 != yyyy2)
{
try
{
if (mm1 >12)
{
yyyy1++;
mm1=1;
}
else
{
day1++;
String syyyy1=Integer.toString(yyyy1);
String smm1=Integer.toString(mm1);
String sday1=Integer.toString(day1);
if (smm1.length()==1)
{
smm1="0"+smm1;
}
if (sday1.length()==1)
{
sday1="0"+sday1;
}
tempDate = syyyy1 +
smm1 +
sday1;
df.parse(tempDate);
list.add(tempDate);
}
}catch(Exception e)
{
mm1++;
day1=0;
}
}
//add some element still the year and month same
while(mm1 != mm2)
{
try
{
day1++;
String syyyy1=Integer.toString(yyyy1);
String smm1=Integer.toString(mm1);
String sday1=Integer.toString(day1);
if (smm1.length()==1)
{
smm1="0"+smm1;
}
if (sday1.length()==1)
{
sday1="0"+sday1;
}
tempDate = syyyy1 +
smm1 +
sday1;
df.parse(tempDate);
list.add(tempDate);
}catch(Exception e)
{
mm1++;
day1=0;
}
}
//add some element still the all same
while(day1 != day2)
{
try
{
day1++;
String syyyy1=Integer.toString(yyyy1);
String smm1=Integer.toString(mm1);
String sday1=Integer.toString(day1);
if (smm1.length()==1)
{
smm1="0"+smm1;
}
if (sday1.length()==1)
{
sday1="0"+sday1;
}
tempDate = syyyy1 +
smm1 +
sday1;
df.parse(tempDate);
list.add(tempDate);
}catch(Exception e)
{
;
}
}
return list.toArray();
}
public static void main(String[] args) throws Exception
{
Object[] value = getValue("20030227","20030302");
// you can translate the object array to String array
for (int i=0;i<value.length ;i++ )
{
System.out.println(value[i]);
}
}
}
回复
AllError 2003-04-04
多了两个}
回复
AllError 2003-04-04
改进版:
package com.turnpage;

/**
* Created by IntelliJ IDEA.
* User: Administrator
* Date: 2003-4-4
* Time: 11:53:30
* To change this template use Options | File Templates.
*/
import java.util.Calendar;
import java.util.Vector;
import java.text.SimpleDateFormat;
public class test {

public static void main(String args[]){
Calendar scal = Calendar.getInstance();
Calendar cal = Calendar.getInstance();
Vector list=new Vector();
String str="";
int syear=2003,smonth=4,sday=9;
int eyear=2004,emonth=2,eday=7;
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
for(int m=sday;m<((eyear-syear)*12+(emonth-smonth)+(eday-sday)+1);m++){
cal.set(syear,smonth,m);
str=formatter.format(cal.getTime()) ;
list.addElement(str);
}
}
}
//有了list,剩下的不用我说了吧



}
}
回复
moumouren 2003-04-04
String str1 = "20030315";
String str2 = "20030404";

Calendar calendar1 = Calendar.getInstance(TimeZone.getTimeZone("CTT"));
calendar1.set(Calendar.YEAR,Integer.parseInt(str1.substring(0,4)));
calendar1.set(Calendar.MONTH,Integer.parseInt(str1.substring(4,6))-1);
calendar1.set(Calendar.DAY_OF_MONTH,Integer.parseInt(str1.substring(6,8)));

SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
List list = new ArrayList();
Date date1 = calendar1.getTime();
date1.setTime(date1.getTime()+(long)24*60*60*1000);
while(!(format.format(date1)).equals(str2)){
list.add(format.format(date1));
date1.setTime(date1.getTime()+(long)24*60*60*1000);
}
String[] result = new String[list.size()];
list.toArray(result);
for(int i=0;i<result.length;i++){
System.out.println(result[i]);
}
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2003-04-04 10:54
社区公告
暂无公告