-1

I'm having a weird situation with Java Calendar. I'm using dozer mapper to map the objects.

I want to write a method that will convert this object to the following format. yyyy-MM-dd'T'HH:mm:ss.SSS'Z'

say element 2010-11-11T09:30:47.000Z

public Calender getValue(Date source,Calender c) {
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
calendar.setTime(source);
return calendar;
}

When I run the program, it is printing 2010-11-11T04:00:47.000Z - Because we are setting the Timezone to be GMT, (9.30 - 5.30 = 4.00)

I want my object to have same format and value.if I don't set TimeZone to GMT, it will show as 2008-11-21T09:30:47.000+05:30.

I want it as 2010-11-11T09:30:47.000Z.

I tried added 5.30 to calender.

    calendar.add(Calendar.HOUR, 5);
    calendar.add(Calendar.MINUTE, 30)

then it works.But if this is ran from any other place, difference won't be 5.30.So I cannot add 5.30 to calenderget

Is there any way to get rid of this problem? I want to return Calender object.

Any suggestions or help would be much appreciated

2 Answers2

2

Use a pattern. F.E:

String pattern = "yyyy-MM-dd'T'HH:mm:ssZ";
DateTimeFormatter dtf = DateTimeFormat.forPattern(pattern);

Also,

SimpleDateFormat dateformatyyyyMMdd = new SimpleDateFormat("yyyyMMdd");
String date_to_string = dateformatyyyyMMdd.format(dateNow);
aran
  • 10,978
  • 5
  • 39
  • 69
0

you can use SimpleDateFormat like this.

   SimpleDateFormat formatter, FORMATTER;
   formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
   String oldDate = "2011-03-10T11:54:30.207Z";
   Date date = formatter.parse(oldDate.substring(0, 24));
   FORMATTER = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss.SSS");
   System.out.println("OldDate-->"+oldDate);
   System.out.println("NewDate-->"+FORMATTER.format(date));

Output OldDate-->2011-03-10T11:54:30.207Z NewDate-->10-Mar-2011 11:54:30.207

sherin
  • 1,091
  • 2
  • 17
  • 27