各位师兄:
   大家好,我知道new Date()是当前的时间,但我想取得以下时间
1、昨天的时间(比如昨天的16点0分0秒)
2、上个月的20号的00点0分0秒
3、去年这个月的20号的00点0分0秒。这里取得的都是应该是Date类型的数据。怎么取得?
如果我还需要将其转化为String类型如(YYYY-MM-DD HH:SS:MM)又应该怎么写呢?
请指教,希望按顺序写出(Date类型如何取得和如何将其转化),谢谢了。

解决方案 »

  1.   

    import java.text.SimpleDateFormat;
    import java.text.*;
    import java.util.Date;
    public class Format {    public static String getDateTime()
        {
         SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
         java.util.Date Now=new java.util.Date();
         String NDate=formatter.format(Now);
         return NDate;
        }
        public static String getStrDate(String DateString){
         return DateString.substring(0,10);
        }    
        public static String getStrDateTime(){
         return StringUtils.replace(StringUtils.replace(StringUtils.replace(getDateTime(),":",""),"-","")," ","");
         }
        public static boolean compareTo(String  last,String now){
         try{
             DateFormat formatter=DateFormat.getDateInstance();
         Date temp1=formatter.parse(last);
         Date temp2=formatter.parse(now);
         if(temp1.after(temp2))
         return false;
         else if(temp1.before(temp2))
         return true;
         }
         catch(ParseException e)
         {
         e.printStackTrace();
         }
         return false;
        }    /**
         * 字符串替换,将 source 中的 oldString 全部换成 newString
         *
         * @param source 源字符串
         * @param oldString 老的字符串
         * @param newString 新的字符串
         * @return 替换后的字符串
         */
        public static String Replace(String source, String oldString, String newString) {
            StringBuffer output = new StringBuffer();        int lengthOfSource = source.length();   // 源字符串长度
            int lengthOfOld = oldString.length();   // 老字符串长度        int posStart = 0;   // 开始搜索位置
            int pos;            // 搜索到老字符串的位置        while ((pos = source.indexOf(oldString, posStart)) >= 0) {
                output.append(source.substring(posStart, pos));            output.append(newString);
                posStart = pos + lengthOfOld;
            }        if (posStart < lengthOfSource) {
                output.append(source.substring(posStart));
            }        return output.toString();
        }    /**
         * 将字符串格式化成 HTML 代码输出
         * 只转换特殊字符,适合于 HTML 中的表单区域
         *
         * @param str 要格式化的字符串
         * @return 格式化后的字符串
         */
        public static String toHtmlInput(String str) {
            if (str == null)    return null;        String html = new String(str);        html = Replace(html, "&", "&amp;");
            html = Replace(html, "<", "&lt;");
            html = Replace(html, ">", "&gt;");        return html;
        }    /**
         * 将字符串格式化成 HTML 代码输出
         * 除普通特殊字符外,还对空格、制表符和换行进行转换,
         * 以将内容格式化输出,
         * 适合于 HTML 中的显示输出
         *
         * @param str 要格式化的字符串
         * @return 格式化后的字符串
         */
        public static String toHtml(String str) {
            if (str == null)    return null;        String html = new String(str);        html = toHtmlInput(html);
            html = Replace(html, "\r\n", "\n");
            html = Replace(html, "\n", "<br>\n");
            html = Replace(html, "\t", "    ");
            html = Replace(html, "  ", " &nbsp;");        return html;
        }    /**
         * 将普通字符串格式化成数据库认可的字符串格式
         *
         * @param str 要格式化的字符串
         * @return 合法的数据库字符串
         */
        public static String toSql(String str) {
            String sql = new String(str);
            return Replace(sql, "'", "''");
        }
    //    public static void main(String[] args) 
    //    {
    //     System.out.println(Format.getStrDate(Format.getDateTime()));
    //    }
      
    }
      

  2.   

    Date-->SimpleDateFormat--->Calendar--->getMonth--->++?--?
      

  3.   

    TO laiseeme(huihui) 
      谢了,不过你的类好象还不能完全实现吧
    ===========================================
      

  4.   


    Calendar c=Calendar.getInstance();
    c.set(int field,int value);
    c.getTime();
    来设置时间,并得到Date转化时间用SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");来格式化
      

  5.   

    Calendar c=Calendar.getInstance();
        Date now=new Date();
           System.out.println(now.getYear()+"年"+now.getMonth());
       c.set(Calendar.MONTH, now.getMonth()-1);///设置上个月    c.set(Calendar.YEAR, now.getYear()-1);//设置前一年
       c.getTime();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   
        String NDate=format.format(c.getTime()); 
         System.out.println (NDate);
    ==================================
      奇怪设置上个月的正常,但设置前一年的却出错了。
    出来的结果是:106年11
    0106-01-25 11:34:20  而且最晕的是,我再打印 System.out.println(now.getYear()+"年"+now.getMonth());
    却一直是106年11。怎么回事?????
      

  6.   

    同时搜网(www.tongshisou.com)是一个很有创意的搜索网站,是目前最易用,最方便的整合搜索网站,本身不做搜索却把业界的搜索引擎聚合在自己网站中,包括百度,GOOGLE,YAHOO,中搜,爱问,搜狗,奇虎,腾讯搜搜,OPENV,TVIX,有网页 新闻 图片 音乐 视频 地图 工作 餐饮 的搜索,可谓很全哦,搜索起来确实方便,而且很易用,可以把结果同时在一个网页中以分割条形式分开,而且可以分割成2-4个窗口,大家可以一试哦www.tongshisou.com
      

  7.   

    Calendar c=Calendar.getInstance();
    c.setTime(new Date());
        
    int nian=c.get(Calendar.YEAR) ;
    int yue=c.get(Calendar.MONTH)+1;
           System.out.println( nian+"年"+yue);
       c.set( nian-1,yue-1,c.get(Calendar.DAY_OF_MONTH));///设置上个月   
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   
        String NDate=format.format(c.getTime()); 
         System.out.println (NDate);
      

  8.   

    Date类的许多方法都已经过时了,还是用Calendar类吧
      

  9.   

    Calendar c=Calendar.getInstance();
        Date now=new Date();
           System.out.println(now.getYear()+"年"+now.getMonth());
       c.set(Calendar.MONTH, now.getMonth()-1);///设置上个月    c.set(Calendar.YEAR, now.getYear()-1);//设置前一年
       c.getTime();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   
        String NDate=format.format(c.getTime()); 
         System.out.println (NDate);好像DATE里的getMonth等方法已经被Calendar里的方法取代了吧。
      

  10.   

    奇怪设置上个月的正常,但设置前一年的却出错了。
    出来的结果是:106年11
    0106-01-25 11:34:20  而且最晕的是,我再打印 System.out.println(now.getYear()+"年"+now.getMonth());
    却一直是106年11。怎么回事?????年份 y 由整数 y - 1900 表示。
      

  11.   

    1、昨天的时间(比如昨天的16点0分0秒)的实现
    SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");    
            Calendar cal=Calendar.getInstance();
            int yesterday=cal.get(cal.DAY_OF_MONTH)-1;
            cal.set(cal.DAY_OF_MONTH,yesterday);
            Date date=cal.getTime();
            System.out.println(date);
            String today=formatter.format(date).toString();
            System.out.println(today);结果:Tue Dec 26 15:25:02 CST 2006
    2006-12-26 15:25:02 
    today即为结果。
      

  12.   

    2、上个月的20号的00点0分0秒
    SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");    
            Calendar cal=Calendar.getInstance();
            int year=cal.get(cal.YEAR);
            int month=cal.get(cal.MONTH);
            cal.set(year, month-1, 20, 0, 0, 0);
            Date date=cal.getTime();
            System.out.println(date);
            String todate=formatter.format(date).toString();
            System.out.println(todate);
    结果为:Mon Nov 20 00:00:00 CST 2006
    2006-11-20 00:00:003、去年这个月的20号的00点0分0秒。
    SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");    
            Calendar cal=Calendar.getInstance();
            int year=cal.get(cal.YEAR);
            int month=cal.get(cal.MONTH);
            cal.set(year-1, month, 20, 0, 0, 0);
            Date date=cal.getTime();
            System.out.println(date);
            String todate=formatter.format(date).toString();
            System.out.println(todate);
    结果为:Tue Dec 20 00:00:00 CST 2005
    2005-12-20 00:00:00不知道LZ对结果满意不?如满意请结贴。
      

  13.   

    SORRY,不好意思,有事出去了两天。谢谢回复,现在就结帐。
      

  14.   

    晕了,今天怎么回事,一结帐就HTTP500错误???
      

  15.   

    // 1、昨天的时间(比如昨天的16点0分0秒)的实现
    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");    
    Calendar c=Calendar.getInstance();
    c.add(Calendar.DAY_OF_MONTH,-1);
    System.out.println(c.getTime());
    System.out.println(sdf.format(c.getTime()));

    // 2、上个月的20号的00点0分0秒
    c=Calendar.getInstance();
    c.add(Calendar.MONTH,-1);
    System.out.println(c.getTime());
    System.out.println(sdf.format(c.getTime())); // 3、去年这个月的20号的00点0分0秒。
    c=Calendar.getInstance();
    c.add(Calendar.YEAR,-1);
    System.out.println(c.getTime());
    System.out.println(sdf.format(c.getTime()));