一般情况下面你都不需要ParsePosition的,主要作用是可以区分几种不同的格式
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
String date="2004-07-21";
Date myDate=sdf.parse(date);

解决方案 »

  1.   

    ParsePosition is a simple class used by Format and its subclasses to keep track of the current position during parsing. The parseObject method in the various Format classes requires a ParsePosition object as an argument. By design, as you parse through a string with different formats, you can use the same ParsePosition, since the index parameter records the current position. 日期格式化用SimpleDateFormat就可以了
      

  2.   

    谢谢 ChDw,haode,俩位大哥,我刚学java不久,希望以后多多支持
      

  3.   

    前辈,parse(Sring) 已经不用了
    现在用的是 parse(String text, ParsePosition pos),但是
    我  不太明白ParsePosition()中的参数应该怎么写,  
    能告诉我ParsePosition()的作用吗  
        就当是改错,帮帮我把
      

  4.   

    ParsePosition只是一个设置字符串解析开始位置和记录错误信息的简单类而已。
    initial index的值就是你需要解析的日期在提供的字符串中的开始位置。
    error index是当解析日期出错时的位置。如果解析正确,其值为-1,否则是出错位置。Example:
        String strDate = "  222024-07-18";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        // 从第4个字符开始解析strDate
        ParsePosition pp = new ParsePosition(4);
        //pp.setIndex(0);

        Date dt = sdf.parse(strDate, pp);此时可以得到正确的Date,Thu Jul 18 00:00:00 CST 2024
    如果从其它位置开始解析,或者日期不对,或者就是null了。
      

  5.   

    主意index是从0开始的。
    如果strDate="2004-07-18",initial index当置为0。
      

  6.   

    谢谢pking2002 前辈,
    可是我 问的是有没有一种方法,使("yyyy-MM-dd")格式转化成("yyyy,MM,dd")格式的
    如果哪位前辈知道,告诉我好吗?
      

  7.   

    yyyy-MM-dd肯定是个字符串,你把你的这个字符串转化成Date然后用SimpleDateFormat转化成
    yyyy,MM,dd就可以了
      

  8.   

    package iotest;
    import java.text.ParsePosition;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    public class Avector {
    public static void main(String args[]) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    String date = "2004-07-21";
    try {
    Date myDate = sdf.parse(date);
    sdf.applyPattern("yyyy,MM,dd");
    System.out.println(sdf.format(myDate));
    }
    catch (Exception e) {
    e.printStackTrace();
    } }
    }
      

  9.   

    [前辈,parse(Sring) 已经不用了
    现在用的是 parse(String text, ParsePosition pos),但是
    我  不太明白ParsePosition()中的参数应该怎么写,  
    能告诉我ParsePosition()的作用吗  
        就当是改错,帮帮我把]你这个“parse(Sring) 已经不用了”是谁说的,JDK 1.4的Doc中可没说这个方法已经deprecated了