Java Stringbuilder用于格式化日期

Java Stringbuilder用于格式化日期,java,Java,我需要使用Stringbuilder将用户输入的日期从mm/dd/yyyy格式转换为长格式,例如2011年8月20日。此外,在本作业到期之前,我的老师还没有检查Stringbuilder或标记化,所以我不得不上网寻求帮助。我想知道是否有人能帮我弄清楚如何使用split方法,因为我的构造函数接受字符串输入,我需要将字符串标记为三部分,但我想知道split方法是如何实现的。这是我现在的代码: /**Lab 08 * Class CheckDate that stores a user-input

我需要使用Stringbuilder将用户输入的日期从mm/dd/yyyy格式转换为长格式,例如2011年8月20日。此外,在本作业到期之前,我的老师还没有检查Stringbuilder或标记化,所以我不得不上网寻求帮助。我想知道是否有人能帮我弄清楚如何使用split方法,因为我的构造函数接受字符串输入,我需要将字符串标记为三部分,但我想知道split方法是如何实现的。这是我现在的代码:

/**Lab 08
 * Class CheckDate that stores a user-input date as a String in the format mmddyyyy
 */

import java.util.StringTokenizer;
import java.util.Scanner;

public class CheckDate
{
 public int validMonth;
 public int validDay;
 public int validYear;

 Scanner input = new Scanner(System.in);

 public CheckDate(String date)throws InvalidDateException 
 {

      StringTokenizer dateToken = new StringTokenizer(date, "/");

         int validMonth = Integer.parseInt (dateToken.nextToken().trim());
         int validDay = Integer.parseInt (dateToken.nextToken().trim());
         int validYear = Integer.parseInt (dateToken.nextToken().trim());



       if ((validMonth == 4 || validMonth == 6 || validMonth == 9 || validMonth == 11) &&(validDay > 30))
          {
             throw new InvalidDateException("Day value must be greater than 0 and less than 30");
           }
              else
               {
                      setValidString(validDay, validMonth, validYear);
                }

        if (validMonth == 2 && validDay > 28)
           {
            throw new InvalidDateException ("Day value must be greater than 0 and less than 28");
        }
         else if(validMonth > 31)
         {
           throw new InvalidDateException ("Day value must be greater than 0 and less than 31");
         }
         else
         {

            setValidString(validDay, validMonth, validYear);
         }

       if(validMonth < 1 || validMonth > 12)
         {
            throw new InvalidDateException("Month value must be greater than 0 and less than 12");
          }
             else
             {
                  setValidString(validDay, validMonth, validYear);
             }


       if(validYear < 2010)
       {
           throw new InvalidDateException("Year must be greater than 2010");
        }
            else
           {
               setValidString(validDay, validMonth, validYear);
           }
 }//end constructor

  public void setValidString(int day, int month, int year) 
    {

     validDay = day;
     validMonth = month;
     validYear = year; 
    }

    public String getValidString()
    {
      return (validMonth + "/" + validDay + "/" + validYear + "/");
    }

    public String getLongDate()
    {

    }
 }

您使用
StringTokenizer
的解决方案似乎还可以。但以下版本使用:


您使用
StringTokenizer
的解决方案似乎还可以。但以下版本使用:


如果Banthar要给出一个不使用所需类(StringTokenizer)的答案,我将1+他的答案(已经完成),满足他的赌注,并使用SimpleDateFormat提高它。同样,使用这个类可以极大地简化您的代码和生活。比如说,

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class CheckDate2 {
   public static final String SHORT_FORMAT = "MM/dd/yyyy";
   public static final String LONG_FORMAT = "MMMM dd, yyyy";
   private SimpleDateFormat shortFormat = new SimpleDateFormat(SHORT_FORMAT);
   private SimpleDateFormat longFormat = new SimpleDateFormat(LONG_FORMAT);
   private Date date;

   public CheckDate2(String dateText) throws ParseException {
      try {
         date = shortFormat.parse(dateText);
      } catch (ParseException e) {
         // if it fails to parse via the short format, try the long format
         date = longFormat.parse(dateText);
         // if it fails this, the constructor will throw the exception
      }
   }

   public String getShortDate() {
      return shortFormat.format(date);
   }

   public String getLongDate() {
      return longFormat.format(date);
   }

   public Date getDate() {
      return date;
   }

   // Test the code:
   public static void main(String[] args) {
      String test1 = "02/16/2012";
      String test2 = "March 1, 2011";

      String[] tests = {test1, test2};

      for (String test : tests) {
         try {
            System.out.println("Testing: " + test);
            CheckDate2 checkDate = new CheckDate2(test);
            System.out.println("Long Format:  " + checkDate.getLongDate());
            System.out.println("Short Format: " + checkDate.getShortDate());
            System.out.println();
         } catch (ParseException e) {
            e.printStackTrace();
         }
      }
   }
}

如果Banthar要给出一个不使用所需类(StringTokenizer)的答案,我将1+他的答案(已经完成),满足他的赌注,并使用SimpleDateFormat提高它。同样,使用这个类可以极大地简化您的代码和生活。比如说,

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class CheckDate2 {
   public static final String SHORT_FORMAT = "MM/dd/yyyy";
   public static final String LONG_FORMAT = "MMMM dd, yyyy";
   private SimpleDateFormat shortFormat = new SimpleDateFormat(SHORT_FORMAT);
   private SimpleDateFormat longFormat = new SimpleDateFormat(LONG_FORMAT);
   private Date date;

   public CheckDate2(String dateText) throws ParseException {
      try {
         date = shortFormat.parse(dateText);
      } catch (ParseException e) {
         // if it fails to parse via the short format, try the long format
         date = longFormat.parse(dateText);
         // if it fails this, the constructor will throw the exception
      }
   }

   public String getShortDate() {
      return shortFormat.format(date);
   }

   public String getLongDate() {
      return longFormat.format(date);
   }

   public Date getDate() {
      return date;
   }

   // Test the code:
   public static void main(String[] args) {
      String test1 = "02/16/2012";
      String test2 = "March 1, 2011";

      String[] tests = {test1, test2};

      for (String test : tests) {
         try {
            System.out.println("Testing: " + test);
            CheckDate2 checkDate = new CheckDate2(test);
            System.out.println("Long Format:  " + checkDate.getLongDate());
            System.out.println("Short Format: " + checkDate.getShortDate());
            System.out.println();
         } catch (ParseException e) {
            e.printStackTrace();
         }
      }
   }
}

只需查找并使用,就可以为自己省去很多麻烦。只需查找并使用,就可以为自己省去很多麻烦。因此,这样做我就不必标记它了?从技术上讲,它仍然是标记字符串,但如果这样做,就不必使用StringTokenizer。它的工作原理和你版本的第四行差不多。当我尝试这种方式时,我的测试工具没有工作。它不断抛出异常,好像没有捕捉到正确的信息。这两者之间有一些微妙的区别。例如,您的版本使用
trim
去除空白。您的测试可能取决于此。无效输入的行为也会有所不同。是的,当我为当月输入06时,它会捕获0,并在我使用您版本的split方法时引发异常。因此,这样做我就不必标记它了?从技术上讲,它仍然标记字符串,但如果这样做,您就不必使用StringTokenizer。它的工作原理和你版本的第四行差不多。当我尝试这种方式时,我的测试工具没有工作。它不断抛出异常,好像没有捕捉到正确的信息。这两者之间有一些微妙的区别。例如,您的版本使用
trim
去除空白。您的测试可能取决于此。无效输入的行为也将不同。是的,当我输入当月的06时,它会抓取0,并在我使用您版本的split方法时引发异常。她希望我们使用标记器和split方法,以便将字符串输入的结果转换为mm/dd/yyyy。她希望使用Stringbuilder转换日期。嗯,她希望我们使用标记器和split方法,以便从输入到mm/dd/yyyy的字符串中获得结果。她希望使用Stringbuilder转换日期。
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class CheckDate2 {
   public static final String SHORT_FORMAT = "MM/dd/yyyy";
   public static final String LONG_FORMAT = "MMMM dd, yyyy";
   private SimpleDateFormat shortFormat = new SimpleDateFormat(SHORT_FORMAT);
   private SimpleDateFormat longFormat = new SimpleDateFormat(LONG_FORMAT);
   private Date date;

   public CheckDate2(String dateText) throws ParseException {
      try {
         date = shortFormat.parse(dateText);
      } catch (ParseException e) {
         // if it fails to parse via the short format, try the long format
         date = longFormat.parse(dateText);
         // if it fails this, the constructor will throw the exception
      }
   }

   public String getShortDate() {
      return shortFormat.format(date);
   }

   public String getLongDate() {
      return longFormat.format(date);
   }

   public Date getDate() {
      return date;
   }

   // Test the code:
   public static void main(String[] args) {
      String test1 = "02/16/2012";
      String test2 = "March 1, 2011";

      String[] tests = {test1, test2};

      for (String test : tests) {
         try {
            System.out.println("Testing: " + test);
            CheckDate2 checkDate = new CheckDate2(test);
            System.out.println("Long Format:  " + checkDate.getLongDate());
            System.out.println("Short Format: " + checkDate.getShortDate());
            System.out.println();
         } catch (ParseException e) {
            e.printStackTrace();
         }
      }
   }
}