0

i need a to transfer the current date time into ios8601 format, something like :

date and time in format: 01 Jan 2014 BST 12:00 PM

eventually on my jsp , i wish to put the formated date in

<time datetime="{date and time in ISO8601}">{date and time in format: 01 Jan 2014 BST 12:00 PM}</time>

my current try out:

    TimeZone tz = TimeZone.getTimeZone("BST");
    DateFormat df = new SimpleDateFormat("dd-MM-yyyy'T'HH:mm'Z'");
    df.setTimeZone(tz);
    String nowAsISO = df.format(new Date());

but the result doesnt look like what i expect to get......

sefirosu
  • 2,558
  • 7
  • 44
  • 69
  • Do you want ISO-8601 format or do you want `01 Jan 2014 BST 12:00 PM`? The two are not the same. – Duncan Jones Sep 01 '14 at 14:31
  • 01 Jan 2014 BST 12:00 PM ,, this is the one i want.. plz provide some code example is possible, cheers – sefirosu Sep 01 '14 at 14:33
  • @seph That example is not even close to any of the [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) formats. None of the formats use a name-of-month, nor do they use 3 letter time zone codes. A date-time would be in a format like `YYYY-MM-DDTHH:MM:SS.SSS±HH:MM`, such as `2015-05-31T21:00:17+02:00` or `2015-05-31T19:00:17Z`. – Basil Bourque Jun 02 '15 at 01:37
  • Out of curiosity, why did this Java question get marked as a duplicate of a C# question? I don't see how the linked question does anything to help resolve this one, given that the answers only offer .NET library calls. – Soren Bjornstad Aug 26 '20 at 15:11

1 Answers1

2

This pattern will give you what you need:

SimpleDateFormat format = new SimpleDateFormat("dd MMM yyyy zzz HH:mm a");
format.setTimeZone(TimeZone.getTimeZone("Europe/London"));
System.out.println(format.format(new Date()));

Prints:

01 Sep 2014 BST 15:37 PM

Note: I've assumed you were looking for BST = British Summer Time, which should be expressed as above. If you wanted Bangladesh time, use Asia/Dhaka as the timezone designator. Also note that British Summer Time is not in effect on 1st January (as quoted in your example).

Duncan Jones
  • 67,400
  • 29
  • 193
  • 254