Reading system time in CST time zone using Java -
i trying read system date in cst time zone using java. tried below code whenever use formatter.parse()
returning time in est time zone.
private date gettodayincst() { calendar currentdate = calendar.getinstance(); dateformat formatter = new simpledateformat("dd-mm-yyyy hh:mm:ss"); timezone obj = timezone.gettimezone("cst"); formatter.settimezone(obj); string today = formatter.format(currentdate.gettime()); try { return formatter.parse(today); } catch (parseexception e) { e.printstacktrace(); } return null; }
java.util.date
objects not contain timezone information - cannot set timezone on date object. thing date object contains number of milliseconds since "epoch" - 1 january 1970, 00:00:00 utc.
if want set timezone try way
simpledateformat format = new simpledateformat("yyyy-mm-dd hh:mm:ss z"); format.settimezone(timezone.gettimezone("cst")); system.out.println(format.format(new date()));
Comments
Post a Comment