I'm trying to convert string timestamp having timezone in it to a timestamp with the same timezone. However, when executing the below code, I'm getting default system timestamp. Can someone help me on this. Below is the code I'm trying.
try {
Date dt = new Date();
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
f.setTimeZone(TimeZone.getTimeZone("GMT"));
String dateString = f.format(dt);
System.out.println("DateString: "+dateString);
Date parsedDate = f.parse(dateString);
System.out.println("ParsedDate: "+parsedDate);
Timestamp timestamp = new Timestamp(parsedDate.getTime());
System.out.println("Timestamp: "+timestamp);
}catch (Exception e) {
// TODO: handle exception
}
When executing the above code, I get below result:
DateString: 2021-03-26T06:57:05.982+0000
ParsedDate: Fri Mar 26 12:27:05 IST 2021
Timestamp: 2021-03-26 12:27:05.982
But I have to get output in Timestamp as 2021-03-26T06:57:05.982+0000