Is there something wrong with SimpleDateFormat year and timezone formating

I'm up to my wits end on this annoying problem. Basically couldn't fix this for a long time.

     java.util.Calendar calendar_now = java.util.Calendar.getInstance();
     java.util.Calendar calendar_entry = java.util.Calendar.getInstance();
     java.util.Date dt = new Date();
     java.text.SimpleDateFormat formatter;
  try{
     // if(this.Time.length() == 0) {this.Time = "00:00";}
     //this.Time = "00:00";
     // System.out.println("*" + this.Time + "*");
     if((this.Time.substring(this.Time.length() - 2) == "am")||(this.Time.substring(this.Time.length() - 2) == "pm"))
     {
      formatter = new SimpleDateFormat("yyyy E MMM d H:mmaa z",Locale.US);
       dt = (Date)formatter.parse(calendar_now.get(java.util.Calendar.YEAR) + " " + this.Date + " " + this.Time + " EST");
      //calendar_entry.setTimeZone(java.util.TimeZone.getTimeZone("America/New_York"));
     }
      calendar_entry.setTime(dt);
      if (calendar_entry.get(java.util.Calendar.MONTH) < calendar_now.get(java.util.Calendar.MONTH)){
       calendar_entry.set(java.util.Calendar.YEAR,calendar_now.get(java.util.Calendar.YEAR) + 1);
      }
    else{
       calendar_entry.set(java.util.Calendar.YEAR,calendar_now.get(java.util.Calendar.YEAR));
    }
//    calendar_entry.setTimeZone(java.util.TimeZone.getTimeZone("GMT"));
      System.out.println(calendar_now.get(java.util.Calendar.YEAR) + " " + this.Date + " " + this.Time + " EST");
      System.out.println(dt.toString());
      System.out.println(calendar_entry);

This produces:

2010 Fri Oct 1 10:00am EST
Thu Oct 01 10:00:00 SGT 1970
java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=true,lenient=true,zone=sun.uti
l.calendar.ZoneInfo[id="GMT",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null],fi
rstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2010,MONTH=9,WEEK_OF_YEAR=40,WEEK_OF_MONTH=1,DAY_
OF_MONTH=1,DAY_OF_YEAR=274,DAY_OF_WEEK=5,DAY_OF_WEEK_IN_MONTH=1,AM_PM=0,HOUR=2,HOUR_OF_DAY=2,MINUTE=
30,SECOND=0,MILLISECOND=0,ZONE_OFFSET=0,DST_OFFSET=0]

Question is... WHY?


I have no idea what you have and what you'd like to end up with, but I see at least one major mistake in the code. In this line

if((this.Time.substring(this.Time.length() - 2) == "am")||(this.Time.substring(this.Time.length() - 2) == "pm"))

you're comparing String by reference, not by value. You should be using Object#equals() whenever you want to compare two different objects. A String is namely an object, not a primitive like int , boolean , etc, for which the == would work as intented and expected.

if((this.Time.substring(this.Time.length() - 2).equals("am"))||(this.Time.substring(this.Time.length() - 2).equals("pm")))

However, I'd suggest to use the String#endsWith() method instead. That's not only more concise, but also makes the code more self-explaining:

if (this.Time.endsWith("am") || this.Time.endsWith("pm")) 

Having said that, there's undoubtely a more elegant way for whatever you'd like to achieve, but since the functional requirements are unclear, it's impossible to give you a kickoff example of that.


Going through your code it looks like following lines of code (Which you expect for formatting) is never executed.

 formatter = new SimpleDateFormat("yyyy E MMM d H:mmaa z",Locale.US);
 dt = (Date)formatter.parse(calendar_now.get(java.util.Calendar.YEAR) + " " + this.Date + " " + this.Time + " EST");

As rightly pointed out by BalusC, this is due to invalid conditional statement for String comaprison. ie you are using "==" instead of ".equals" method. Your problem would be solved by correcting same as suggested by BalusC by using :

if (this.Time.endsWith("am") || this.Time.endsWith("pm")) 

instead of:

if((this.Time.substring(this.Time.length() - 2) == "am")||(this.Time.substring(this.Time.length() - 2) == "pm"))
链接地址: http://www.djcxy.com/p/3088.html

上一篇: 日历api单独显示日期

下一篇: SimpleDateFormat年份和时区格式有问题吗?