views:

68

answers:

2

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?

+1  A: 

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.

BalusC
Ok now the code certainly looks more beautiful. Perhaps I should have asked a more specific question. Why is the Year and the TimeZone not modified in the Date object just like the original string???? Its supposed to give 2010 EST and its giving SGT 1970 instead?
Because the `if` block is never been entered.
BalusC
thanks a lot it works now. going to try and figure out why only the time/date works.
i'm trying to vote up your answer but i can't seem to do it.
You can only vote when you've at least 15 reputation. You can however mark the most helpful answer accepted by clicking the checkmark.
BalusC
+1  A: 

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. i.e. 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"))
YoK
Ok now the code certainly looks more beautiful. Perhaps I should have asked a more specific question. Why is the Year and the TimeZone not modified in the Date object just like the original string???? Its supposed to give 2010 EST and its giving SGT 1970 instead?
That is what I have answered. You are setting date time and timezone in "dt" object withing conditional block which never executes. Don't use "==" for String comparision, Condition is always false due to "==" usage in your case. Please try condition as suggested in my answer, it should work as you expect.
YoK
thanks a lot it works now. going to try and figure out why only the time/date works.
i'm trying to vote up your answer but i can't seem to do it.