Reputation: 65
I am in timeZone +05:30 hrs and I want the time zone of Europe, so I am trying this:-
TimeZone tmz=TimeZone.getTimeZone("Europe/Zurich");
Calendar calender=new GregorianCalendar(tmz);
Date date=calender.getTime();
String datestr=new SimpleDateFormat("yyyy-mm-dd hh:mm:ss").format(date);
System.out.println(datestr+" CST");
But I am getting my timezone's time instead
Upvotes: 0
Views: 364
Reputation: 12817
If you're using Java8 with new Clock API you can get the same using below
ZoneId zone = ZoneId.of("Europe/Zurich");
LocalTime localTime = LocalTime.now(zone);
and DateTimeFormatter
has many options to format the date and time
EDIT
LocalDateTime dateTime = LocalDateTime.now(zoneId);
For getting date and time,
Upvotes: 0
Reputation: 338181
The answer by Jon Skeet is correct and direct, and should be accepted.
The answer by Saravana follows Skeet’s suggestion to use the new java.time framework. But that Answer has time-of-day and the Question asked for date-time. Thus this Answer here.
The java.time framework is bundled with Java 8 and later. See Tutorial. These new classes are inspired by Joda-Time, defined by JSR 310, and extended by the ThreeTen-Extra project. They are a vast improvement over the troublesome old classes, java.util.Date/.Calendar et al.
Current date-time is easy to get. Specify your desired/expected time zone by name. If omitted, your your JVM’s current default time zone is implicitly applied.
ZoneId zoneId = ZoneId.of ( "Europe/Zurich" );
ZonedDateTime nowZurich = ZonedDateTime.now ( zoneId );
You can generate a String representation of that date-time value. By default, the toString
method uses the standard ISO 8601 formatting but extends the standard by appending the zone name in brackets.
String outputStandard = nowZurich.toString (); // ISO 8601 format, extended by appending name of zone in brackets.
Alternatively, you can use a localized format, or even define your own format. Specify a Locale.
Locale localeFrenchSwitzerland = new Locale.Builder ().setLanguage ( "fr" ).setRegion ( "CH" ).build ();
DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime ( FormatStyle.FULL ).withLocale ( localeFrenchSwitzerland );
String outputFrenchSwitzerland = nowZurich.format ( formatter );
Dump to console.
System.out.println ( "outputStandard: " + outputStandard );
System.out.println ( "outputFrenchSwitzerland: " + outputFrenchSwitzerland );
When run.
outputStandard: 2015-10-11T02:39:58.287+02:00[Europe/Zurich]
outputFrenchSwitzerland: dimanche, 11. octobre 2015 02.39. h CEST
If you really need a java.util.Date for interoperability with other classes not yet updated for java.time, convert.
java.util.Date date = java.util.Date.from ( nowZurich.toInstant () );
Upvotes: 0
Reputation: 1499770
You need to set the time zone in the SimpleDateFormat
. A Date
value doesn't have a time zone, so your initial code is fairly pointless - you could just call new Date()
.
Note that your format string is incorrect too - you're using minutes instead of months, and you're using a 12-hour clock which almost certainly isn't what you want.
I suspect your code should be:
TimeZone tmz = TimeZone.getTimeZone("Europe/Zurich");
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
format.setTimeZone(tmz);
String datestr = format.format(new Date());
As an aside, if you possibly can, I would avoid using all these classes - use Joda Time if you're stuck on Java 7 or earlier, and the java.time
package if you're using Java 8. They're far, far better date/time APIs.
Upvotes: 1
Reputation: 1435
You can try this..
To display the time according to the time zone you've set for the Calendar
, you could use a DateFormat
object and do something like the following:
import java.text.DateFormat;
import java.util.*;
public class PrintDate
{
public static void main (String[] args)
{
TimeZone tmz=TimeZone.getTimeZone("Europe/Zurich");
DateFormat df = DateFormat.getDateTimeInstance();
df.setTimeZone(tmz);
System.out.println("Current time in Europe--> " +
df.format(Calendar.getInstance(tmz).getTime()));
}
}
Upvotes: 0