[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 349
  • Last Modified:

SimpleDateFormat pattern to produce 2002-01-02T09:30:47-05:00

Hi Guys,

I want to format a date such as 2002-01-02T09:30:47-05:00 using the java.text.SimpleDateFormat class.  

The closest I have come to this format is using this pattern:
yyyy-MM-dd'T'HH:mm:ssZ
This results in: 2007-10-12T11:01:26+0200

Carefully note the +0200 part, I need to have a colon between the hours and minutes.  That is +02:00.
My desired result after formatting should then be: 2007-10-12T11:01:26+02:00

How can I modify this pattern?

Thanks M.A
0
maskedavenger
Asked:
maskedavenger
  • 4
  • 4
  • 3
4 Solutions
 
objectsCommented:
you'll need to use two formats one for the date, and the other the timezone, and edit the formatted timezone (or just build it yourslef without SDF)
0
 
objectsCommented:
or you could use one, and then insert the : yourself in formatted string

0
 
objectsCommented:
something like:

String s = sdf.format(time);
s = new StringBuffer(s).insert(s.length()-2, ':').toString();
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
maskedavengerAuthor Commented:
This was my original solution:

  private static SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
   
  /**
   * Convert date into a formatted string
   */
  public static String formatToString(Date dateToFormat) {
    StringBuffer buffer = new StringBuffer(dateFormatter.format(dateToFormat));
   
    // We have to manually insert a colon
    // i.e 2002-01-02T09:30:47-0500 becomes 2002-01-02T09:30:47-05:00
    buffer.insert(buffer.length() - 2, ':');
   
    return buffer.toString();
  }

I was hoping there was another solution though :)
0
 
objectsCommented:
no better way that I'm aware of without using a 3rd party formatter
0
 
CEHJCommented:
>>I was hoping there was another solution though :)

What sort of thing did you have in mind?
0
 
CEHJCommented:
You *could* do it in one line if that's the sort of thing you meant...
0
 
maskedavengerAuthor Commented:
Well looking at the http://java.sun.com/j2se/1.4.2/docs/api/java/text/SimpleDateFormat.html#timezone javadocs.

I was thinking there's perhaps a way of using the z character in such a way that it produces the desired result.
0
 
CEHJCommented:
>>I was thinking there's perhaps a way of using the z character in such a way that it produces the desired result.

No, because unfortunately the zone info is monolithic as opposed to atomic.
0
 
maskedavengerAuthor Commented:
I guess in that case it might be feasible to extend the SimpleDateFormat class and implement one's own zoning logic ... even if it means simply adding a colon to a string :)
0
 
CEHJCommented:
Yes. You'd get the offset in milliseconds and format as you wish
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now