I need to change the IST to GMT in given code below :
import java.text.SimpleDateFormat;
import java.util.Date;
public class StringToDateExample1 {
public static void main(String[] args)throws Exception {
String sDate1="31/12/1998";
Date date1=new SimpleDateFormat("dd/MM/yyyy").parse(sDate1);
System.out.println(sDate1+"\t"+date1);
}
}
Output :31/12/1998 Thu Dec 31 00:00:00 IST 1998
I need GMT Time please help!!
You have to add time zone in date format 'T':
SimpleDateFormat isoFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
isoFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
System.out.println(sDate1+"\t"+isoFormat.parse("2010-05-23T09:01:02"));
You are using terrible date-time classes that were supplanted years ago by the modern java.time classes defined in JSR 310.
Parse input string as a LocalDate
.
String input = "31/12/1998" ;
DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MM/uuuu" ) ;
LocalDate ld = LocalDate.parse( input , f ) ;
Apparently you want to represent the first moment of that day in UTC.
OffsetDateTime odt = OffsetDateTime.of( ld , LocalTime.MIN , ZoneOffset.UTC ) ;
To generate a string in a custom format, use the DateTimeFormatter
class. That has been covered many many times already on Stack Overflow, I'll not cover that part.
The code for this :
String sDate1 = "31/12/1998";
SimpleDateFormat dateformat = new SimpleDateFormat("dd/MM/yyyy");
dateformat.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println(sDate1 + "\t" + dateformat.parse(sDate1));
Output :
31/12/1998 Thu Dec 31 05:30:00 IST 1998
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.