现在有这样一个问题,我从xml中读到日期的格式是:
YYYY-MM-DDThh:mm:ss
其中T是timezone的信息
现在我想要做的是从这个string中得到Date型的信息
请问有没有什么现成的方法呢?
谢谢

解决方案 »

  1.   

    用SimpleDateFormat试试,不行就只能分割字符串了。
      

  2.   

    String s="2006-05-31Thh:mm:ss";
    System.out.println(s.substring(0, 10));
      

  3.   

    呵呵,经常碰到把一个字符串转成一个Date的问题,其实就3句话:String myString = "2005/12/20";SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd", Locale.CHINA);
    Date d = sdf.parse(myString);下面给你个例子:把字符串"Tue Oct 18 04:11:56 CST 2005"转换成时间。
    package formatdatetime;import java.text.*;
    import java.util.*;/**
     * @author Administrator
     *
     * TODO 要更改此生成的类型注释的模板,请转至
     * 窗口 - 首选项 - Java - 代码样式 - 代码模板
     */
    public class StringToDateTimeTest { public static void main(String[] args) {  String myString = "Tue Oct 18 04:11:56 CST 2005";
      //String myString = "2005/12/20";
      try {
       SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
       //SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd", Locale.CHINA);
       Date d = sdf.parse(myString);
       Calendar c = Calendar.getInstance();
       c.setTime(d);
       String s = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
       System.out.println(s);
       
      } catch (Exception e) {
       e.printStackTrace();
      }
     }
    }
      

  4.   

    你看看sdk的文档中关于SimpleDateFormat的构造函数吧
      

  5.   

    SimpleDateFormat 配合着字符串截取就可以了import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.GregorianCalendar;import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;/**
     * Description:ActionForm的基类
     * Copyright: Copyright (c) 2005
     * Company: 
     * @author quanquan
     * @version 1.0
     */
    public class DateUtil {
        //~ Static fields/initializers    private static Log log = LogFactory.getLog(DateUtil.class);    private static String datePattern = "yyyy-MM-dd";    private static String dataPatterns = "MM/dd/yyyy HH:mm:ss";    private static String timePattern = "HH:mm";    //~ Methods
        // ================================================================    /**
         * Return default datePattern (yyyy/mm/dd)
         * 
         * @return a string representing the date pattern on the UI
         */
        public static String getDatePattern() {
            return datePattern;
        }    /**
         * This method attempts to convert an Oracle-formatted date in the form
         * yyyy/mm/dd to yyyy/mm/dd.
         * 
         * @param aDate
         *            date from database as a string
         * @return formatted string for the ui
         */
        public static final String getDate(Date aDate) {
            SimpleDateFormat df = null;
            String returnValue = "";        if (aDate != null) {
                df = new SimpleDateFormat(datePattern);
                returnValue = df.format(aDate);
            }        return (returnValue);
        }    /**
         * This method generates a string representation of a date/time in the
         * format you specify on input
         * 
         * @param aMask
         *            the date pattern the string is in
         * @param strDate
         *            a string representation of a date
         * @return a converted Date object
         * @see java.text.SimpleDateFormat
         * @throws ParseException
         */
        public static final Date convertStringToDate(String aMask, String strDate)
                throws ParseException {
            SimpleDateFormat df = null;
            Date date = null;
            df = new SimpleDateFormat(aMask);        if (log.isDebugEnabled()) {
                log.debug("converting '" + strDate + "' to date with mask '"
                        + aMask + "'");
            }        try {
                date = df.parse(strDate);
            } catch (ParseException pe) {
                //log.error("ParseException: " + pe);
                throw new ParseException(pe.getMessage(), pe.getErrorOffset());
            }        return (date);
        }    /**
         * This method returns the current date time in the format: yyyy/mm/dd HH:MM
         * a
         * 
         * @param theTime
         *            the current time
         * @return the current date/time
         */
        public static String getTimeNow(Date theTime) {
            return getDateTime(timePattern, theTime);
        }    /**
         * This method returns the current date in the format: yyyy/mm/dd
         * 
         * @return the current date
         * @throws ParseException
         */
        public static Calendar getToday() throws ParseException {
            Date today = new Date();
            SimpleDateFormat df = new SimpleDateFormat(datePattern);        // This seems like quite a hack (date -> string -> date),
            // but it works ;-)
            String todayAsString = df.format(today);
            Calendar cal = new GregorianCalendar();
            cal.setTime(convertStringToDate(todayAsString));        return cal;
        }    /**
         * This method generates a string representation of a date's date/time in
         * the format you specify on input
         * 
         * @param aMask
         *            the date pattern the string is in
         * @param aDate
         *            a date object
         * @return a formatted string representation of the date
         * 
         * @see java.text.SimpleDateFormat
         */
        public static final String getDateTime(String aMask, Date aDate) {
            SimpleDateFormat df = null;
            String returnValue = "";        if (aDate == null) {
                log.error("aDate is null!");
            } else {
                df = new SimpleDateFormat(aMask);
                returnValue = df.format(aDate);
            }        return (returnValue);
        }    /**
         * This method generates a string representation of a date based on the
         * System Property 'dateFormat' in the format you specify on input
         * 
         * @param aDate
         *            A date to convert
         * @return a string representation of the date
         */
        public static final String convertDateToString(Date aDate) {
            return getDateTime(datePattern, aDate);
        }    /**
         * This method converts a String to a date using the datePattern
         * 
         * @param strDate
         *            the date to convert (in format yyyy/mm/dd)
         * @return a date object
         * 
         * @throws ParseException
         */
        public static Date convertStringToDate(String strDate)
                throws ParseException {
            Date aDate = null;        try {
                if (log.isDebugEnabled()) {
                    log.debug("converting date with pattern: " + datePattern);
                }            aDate = convertStringToDate(datePattern, strDate);
            } catch (ParseException pe) {
                log.error("Could not convert '" + strDate
                        + "' to a date, throwing exception");
                pe.printStackTrace();
                throw new ParseException(pe.getMessage(), pe.getErrorOffset());        }        return aDate;
        }    /**
         * 将时间转换为年/月/日 时:分:秒格式
         * 
         * @param date
         *            日期,传入参数new Date();
         * @return 格式化后的日期
         * @throws ParseException
         *             转换异常
         */
        public static Date covertDateToDateWithSec(Date date) throws ParseException {
            GregorianCalendar calendar = new GregorianCalendar();
            calendar.setTime(date);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dataPatterns);
            return simpleDateFormat.parse((calendar.get(Calendar.MONTH) + 1) + "/"
                    + calendar.get(Calendar.DATE) + "/"
                    + calendar.get(Calendar.YEAR) + " "
                    + calendar.get(Calendar.HOUR_OF_DAY) + ":"
                    + calendar.get(Calendar.MINUTE) + ":"
                    + calendar.get(Calendar.SECOND));
        }
    /**
     * String 转换成Double
     * 
     * @param str
     * @return
     */
    public static double strToDouble(String str) {
    return Double.parseDouble(str);
    } /**
     * double 转换成String
     * 
     * @param dou
     * @return
     */
    public static String DoubleToStr(double dou) {
    return Double.toString(dou);
    }
    }