我的代码如下,请大家帮忙看看:
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);
        
    }
}

解决方案 »

  1.   

    import java.text.ParseException;
    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);
    }
    }
      

  2.   

    楼上的结果就很正确了,为什么你从毫秒变为YYYY-MM-dd的时候知道用data.setTime();但是从YYYY-MM-dd的时候却不用getTime()呢?
    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 - 毫秒数。
    现成的方法,不用自己写
      

  3.   

    谢谢两位,不过我测了zhuokai() 给我改后的代码跟我之前的一样得不到正确结果,当我传入入值为"2007-04-31"字串时返回给我的结果是"2007-05-01",而传入"2007-03-31"时返回的结果就时正确的,多试几次后发现时而正确时而不正确.还有谁有更好的建议?先谢谢各位啦
      

  4.   

    当我传入入值为"2007-04-31"字串时返回给我的结果是"2007-05-01"
    ================================================================
    我想这种情况你只能自己在程序中控制了,你得保证你传给date的是一个看上去合法合情合理的数据,check也很中要啊,这两个方法很不错了,给你转成05-1,你传一个没有的值04-31,它肯定不会原样给你返回,如果是人,你说04-31,他也会告诉你那事5-1
      

  5.   

    eyesdragon() ( ) 信誉:97    Blog  2007-3-23 23:28:29  得分: 0  
        
    谢谢两位,不过我测了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日
      

  6.   

    这个问题不是你处理错了,而是API理解错了。如果你可以在 getSecondsFromDate 中设置了日期后加一句
    System.out.println(c.getTime());
    就可以很清楚看到问题所在了。