Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

URGENT: String --> Calendar 1

Status
Not open for further replies.

webIntern

Programmer
Nov 22, 2002
40
0
0
US
Is there *any* possible way to convert this string:
2003-12-11T14:29:56.2729751-05:00

to a Calendar object???

I'm getting the string from a SOAP response. The element is "Expires" and the string above is the value. I need to take that string and save it as a type java.util.Calendar property...

*help!?*
 
Here's a simple program that does a conversion:

Code:
import java.text.*;
import java.util.*;

public class Class1 
{
   public Class1()
   {
      String raw = "2003-12-11T14:29:56.2729751-05:00";
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

      // Remove the T from the string
      raw = raw.replace('T',' ');
      System.out.println(&quot;raw = <&quot; + raw + &quot;>&quot;);

      // Truncate the milliseconds at 3 digits
      int index = raw.lastIndexOf(&quot;.&quot;);
      if (index != -1)
      {
         raw = raw.substring(0,index+4);
      }
      System.out.println(&quot;raw = <&quot; + raw + &quot;>&quot;);

      Date d = sdf.parse(raw, new ParsePosition(0));
      System.out.println(&quot;d = <&quot; + d + &quot;>&quot;);  

      GregorianCalendar c = new GregorianCalendar();
      c.setTime(d);
   
   }

   public static void main(String[] args)
   {
      Class1 class1 = new Class1();
   }
}

I'm assuming the value after the dot is milliseconds, which I truncated to three digits (otherwise the sdf object starts changing the value of seconds). I don't know what the -5:00 is supposed to be... nanoseconds?
 
Thank you so much for the help!

One question however...Can you tell me why it is necessary to take out the &quot;T&quot;? Does it hold any special significance in the timestamp? I'm concerned because that string is sent via SOAP and it's &quot;original form&quot; is that of type of calendar/date object in .NET. It is a SOAP response for a .NET Service call. i.e...maybe that &quot;T&quot; is supposed to be there?

I hope that makes sense - I'm writing a Java client to access .NET web services...and I'm not exactly a Java expert to begin with...

 
I'm just guessing that the T indicates where the date ends and the time begins. If you haven't seen any other letters there, then I think it's safe to ignore it. You might want to google SOAP date format and investigate further...
 
idarke:

Could I convert the Date instance directly to an instance of Calendar as opposed to GregorianCalendar?

 
Calendar is abstract. I think GregorianCalendar is the only implementation of it. Any method that requires a Calendar object as a parameter will accept GregorianCalendar.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top