我的代码如下,请大家帮忙看看:
package gui.portlet.iqa;import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;import util.IQAUtil;public class test {
public static int getSecondsFromDate(String expireDate){
if(expireDate == null || expireDate.trim().equals(""))
return 0;
Calendar c = Calendar.getInstance();
int expireYear = Integer.parseInt(expireDate.substring(0,4));
int expireMonth = Integer.parseInt(expireDate.substring(5,7));
int expireDay = Integer.parseInt(expireDate.substring(8));
c.set(expireYear, expireMonth, expireDay);
long time1 = c.getTime().getTime();
// c.set(1970,1, 1);
// long time2 = c.getTime().getTime();
return (int)((time1) / 1000);
}
public static String getDateFromSeconds(String seconds){
if(seconds == null || seconds.trim().equals("") || seconds.equals("0"))
return "";
else{
Date date = new Date();
date.setTime(Long.parseLong(seconds) * 1000);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(date);
}
}
public static void main(String[] arg0){
int i = IQAUtil.getSecondsFromDate("2007-01-31");
System.out.println(i);
String str = IQAUtil.getDateFromSeconds(String.valueOf(i));
System.out.println(str);
}
}
package gui.portlet.iqa;import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;import util.IQAUtil;public class test {
public static int getSecondsFromDate(String expireDate){
if(expireDate == null || expireDate.trim().equals(""))
return 0;
Calendar c = Calendar.getInstance();
int expireYear = Integer.parseInt(expireDate.substring(0,4));
int expireMonth = Integer.parseInt(expireDate.substring(5,7));
int expireDay = Integer.parseInt(expireDate.substring(8));
c.set(expireYear, expireMonth, expireDay);
long time1 = c.getTime().getTime();
// c.set(1970,1, 1);
// long time2 = c.getTime().getTime();
return (int)((time1) / 1000);
}
public static String getDateFromSeconds(String seconds){
if(seconds == null || seconds.trim().equals("") || seconds.equals("0"))
return "";
else{
Date date = new Date();
date.setTime(Long.parseLong(seconds) * 1000);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(date);
}
}
public static void main(String[] arg0){
int i = IQAUtil.getSecondsFromDate("2007-01-31");
System.out.println(i);
String str = IQAUtil.getDateFromSeconds(String.valueOf(i));
System.out.println(str);
}
}
解决方案 »
- swing JScrollPane作为类变量
- Java包引用问题(有原代码帖上)
- “数据库驱动必须放在服务器的JVM可以访问的路径下”是什么意思?
- Java序列化的对象,能否用其他语言(例如C++,C#)反序列化读取?
- Dimension的hashcode方法为什么这么写?
- 一个Applet的问题
- synchronized是什么关键字?
- 十万火急,那位知道如何用Properties从配置文件中把中文读入??
- 今天真不走运,在win2000server下,我打开的IE窗口不知道为什么就被关闭了,有十几次了,我打开的不过十来个窗口,今天是第一次发现这个问题,请高人指点。
- 请问一个Applet中TextArea的问题
- List问题求助
- ?????求救....啊....
import java.text.SimpleDateFormat;
import java.util.Date;public class IQAUtil
{
public static int getSecondsFromDate(String expireDate)
{
if (expireDate == null || expireDate.trim().equals(""))
return 0;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try
{
date = sdf.parse(expireDate);
return (int) (date.getTime() / 1000);
}
catch (ParseException e)
{
e.printStackTrace();
return 0;
}
} public static String getDateFromSeconds(String seconds)
{
if (seconds == null)
return "";
else
{
Date date = new Date();
try
{
date.setTime(Long.parseLong(seconds) * 1000);
}catch(NumberFormatException nfe)
{
nfe.printStackTrace();
return "Input string:" + seconds + " not correct, eg:2007-01-31";
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(date);
}
} public static void main(String[] arg0)
{
int i = IQAUtil.getSecondsFromDate("2007-01-31");
System.out.println(i);
String str = IQAUtil.getDateFromSeconds(String.valueOf(i));
System.out.println(str);
}
}
getTime
public long getTime()返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数。 返回:
自 1970 年 1 月 1 日 00:00:00 GMT 以来此日期表示的毫秒数。--------------------------------------------------------------------------------setTime
public void setTime(long time)设置此 Date 对象,以表示 1970 年 1 月 1 日 00:00:00 GMT 以后 time 毫秒的时间点。 参数:
time - 毫秒数。
现成的方法,不用自己写
================================================================
我想这种情况你只能自己在程序中控制了,你得保证你传给date的是一个看上去合法合情合理的数据,check也很中要啊,这两个方法很不错了,给你转成05-1,你传一个没有的值04-31,它肯定不会原样给你返回,如果是人,你说04-31,他也会告诉你那事5-1
谢谢两位,不过我测了zhuokai() 给我改后的代码跟我之前的一样得不到正确结果,当我传入入值为"2007-04-31"字串时返回给我的结果是"2007-05-01",而传入"2007-03-31"时返回的结果就时正确的,多试几次后发现时而正确时而不正确.还有谁有更好的建议?先谢谢各位啦 ==========
month, day 最好不要直接用整数,因为一月是0 。
用你的方法设置日期,保存的结果是month ,day对12,当月天数取余之后的日期(也向前进位)。
比如,2007-01-31 = 2007年2月31日
但2月没有31日,多出来了3天,所以
2007-01-31 = 2007年3月3日
2007-12-31 = 2008年1月31日
System.out.println(c.getTime());
就可以很清楚看到问题所在了。