请教大家一个问题,关于日期处理的,急用。请大家多帮忙!
现有一个字符串String str ="Mon Sep 07 15:48:06 CST 2009"怎么把它转化成java.util.Date()格式
显示输出为2009-9-07 15:48:06
用simpleDateFormat.parse(str)转化为日期格式但输出为Mon Sep 07 15:48:06 CST 2009;不是我想要的结果。
请大家多帮忙。非常感谢!!!

解决方案 »

  1.   

    不对吧,你用.parse还是转换成日期型,输出当然是Mon Sep 07 15:48:06 CST 2009这种形式了
    是不是应该先用.parse转换成日期型后,再用simpleDateFormat.format(Date)这个转换成字符串型,就是你要的了,simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      

  2.   

    写错了,不好意思,我是得到一个字符串是string str = "2009-9-07 15:48:06 "
    怎么转换为java.util.Date()格式 显示输出为2009-9-07 15:48:06 
      

  3.   

    你可以直接按string 存进去
    想string转date 比较困难
      

  4.   

    SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    f.parse(str)
      

  5.   

    代码:
     public static void main(String[] s){
       Locale.setDefault(Locale.US);
       DateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
      
       DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      
       try {
       System.out.println(df.format(df1.parse("2009-9-07 15:48:06")));
       System.out.println(df1.format(df.parse("Mon Sep 07 15:48:06 CST 2009")));
    } catch (ParseException e) {
    // TODO 自动生成 catch 块
    e.printStackTrace();
    }
       }
      

  6.   

    我知道用simpleDateFormat.parse(str)转化为日期格式啊
    但输出的格式为Mon Sep 07 15:48:06 CST 2009,在SQL查询时会出错的
    我想要的结果的格式是2009-9-07 15:48:06 
      

  7.   

    换个方式问好了,我现在想得到当前时间的前一天时间。如现在是“2009-9-07 15:48:06 ”
    要得到“2009-9-06 15:48:06 ”得到的格式是java.util.Date()不要String类型的。请大家多帮忙!
      

  8.   


    public static void main(String[] args) throws ParseException {
    SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
     String str = "2009-9-07 15:48:06 " ;
     String time = simple.format(simple.parse(str));
    System.out.println(time); }
    楼主看看这样可以吗?
      

  9.   

    如果是日期类型的话,那你就在写SQL查询语句的时候,转换下即可了,
    没有必要非的先在类中把字符串的日期转换为Date 类型,在做笔记。
    select * from oper_manager
     
    where convert(varchar, end_date,120 )> '2012-01-01' 注: 1:其中的120 的意思就是标示 后面的格式20 或 120 (*)  ODBC 规范 yyyy-mm-dd hh:mm:ss[.fff] 
    2:'2012-01-01' 为自己的参数,可以带时分秒 3:有关convert 的详细 介绍,请参考sql 的帮助文档。
      

  10.   

    首先
    Date date = new Date();//得到当前时间
    long myTime = date.getTime()/1000-24*3600;//date内存放的是一个long型的,表示1970 1月1日 0时0分到现在的毫秒数。
    然后
    date.setTime(myTime);就得到昨天这个时候的时间了。
    如果要用它与数据库比较的话直接用也没问题的(估计你是用的hibernate吧)。
      

  11.   

    都看糊涂了。不明白你要做什么。如果是为了在数据库里查记录用。那你就直接在sql里用函数将字符串转下就可以了。也不用转能什么类型的。直接传字符串就行了
      

  12.   

    上面有点小问题
    date.setTime(myTime*1000);//刚忘了把秒化为毫秒了
      

  13.   

    你API都没用熟悉就玩数据库。好高骛远呀!    public static Date add(Date date, int calendarField, int amount) {
            if (date == null) {
                throw new IllegalArgumentException("The date must not be null");
            }
            Calendar c = Calendar.getInstance();
            c.setTime(date);
            c.add(calendarField, amount);
            return c.getTime();
        }
        public static Date addDays(Date date, int amount) {
            return add(date, Calendar.DAY_OF_MONTH, amount);
        }   你传入负数就可以了。
       这就是前一天的时间析构字符串:    public static Date parseDate(String str, String[] parsePatterns) throws ParseException {
            if (str == null || parsePatterns == null) {
                throw new IllegalArgumentException("Date and Patterns must not be null");
            }
            
            SimpleDateFormat parser = null;
            ParsePosition pos = new ParsePosition(0);
            for (int i = 0; i < parsePatterns.length; i++) {
                if (i == 0) {
                    parser = new SimpleDateFormat(parsePatterns[0]);
                } else {
                    parser.applyPattern(parsePatterns[i]);
                }
                pos.setIndex(0);
                Date date = parser.parse(str, pos);
                if (date != null && pos.getIndex() == str.length()) {
                    return date;
                }
            }
            throw new ParseException("Unable to parse the date: " + str, -1);
        }
      

  14.   


    import java.io.*;
    import java.sql.Timestamp;public class Times{
    public static void main(String[] args) throws Exception {
    String ntime=null;
               Calendar calendar=new GregorianCalendar();
            calendar.setTimeInMillis( System.currentTimeMillis());
            int year1=calendar.get(Calendar.YEAR);
    int month1=calendar.get(Calendar.MONTH)+1;
    int date1=calendar.get(Calendar.DATE);
    int hour1=calendar.get(Calendar.HOUR_OF_DAY);
    int min1=calendar.get(Calendar.MINUTE);
    int sec1=calendar.get(Calendar.SECOND);
    ntime=year1+"-"+month1+"-"+date1+" "+hour1+":"+min1+":"+sec1;
    System.out.println(ntime);
    }
    }
      

  15.   

    本来想转换日期类型查询呢,现在直接在数据库中转换。
    select finsh_time from building where finsh_time between DATE('2009-09-03') and DATE_ADD('2009-09-09',INTERVAL 1 day);
    很感谢大家的帮忙!
      

  16.   

    1.从String获得Date类型数据String pattern = "yyyy-MM-dd HH:mm:ss";
    SimpleDateFormat format = new SimpleDateFormat(pattern);
    Date ret = format.parse(dateStr);
    2.日期回滚        /**
     * 
     * @param now
     * @param year
     * @param month
     * @param day
     * @param hour
     * @param minute
     * @param second
     * @return
     */
    public static Date roll(Date now, int year, int month, int day, int hour,
    int minute, int second) {
    Calendar c = Calendar.getInstance();
    c.setTime(now);
    c.add(Calendar.YEAR, year);
    c.add(Calendar.MONTH, month);
    c.add(Calendar.DATE, day);
    c.add(Calendar.HOUR_OF_DAY, hour);
    c.add(Calendar.MINUTE, minute);
    c.add(Calendar.SECOND, second);
    if (Date.class == now.getClass())
    return c.getTime();
    return newInstance(now.getClass(), c.getTimeInMillis());
    }代码中roll函数为日期回滚类,参数now为原始日期,后续参数为回滚得单位剩下自己搞定