top of page

Time Conversion by country using JavaFX

The Time Zone Converter converts times instantly as you select. Convert between major world cities, countries and timezones.

Typically, you get a TimeZone using getDefault which creates a TimeZone based on the time zone where the program is running. For example, for a program running in Japan, getDefault creates aTimeZone object based on Japanese Standard Time.

You can also get a TimeZone using getTimeZone along with a time zone ID. For instance, the time zone ID for the U.S. Pacific Time zone is America/Los_Angeles. So, you can get a U.S. Pacific Time TimeZone object with:

 TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles");

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>

<AnchorPane prefHeight="416.0" prefWidth="484.0" xmlns="" xmlns:fx="" fx:controller="application.TimeController">
      <Label layoutX="147.0" layoutY="29.0" prefHeight="46.0" prefWidth="190.0" text="Time Conversion">
            <Font size="25.0" />
      <Label layoutX="69.0" layoutY="95.0" prefHeight="37.0" prefWidth="86.0" text="Indian Time">
            <Font size="15.0" />
      <Label fx:id="time_initialize" layoutX="242.0" layoutY="95.0" prefHeight="37.0" prefWidth="197.0" text="date" />
      <Label fx:id="time_result" layoutX="242.0" layoutY="167.0" prefHeight="37.0" prefWidth="197.0" text="date" />
      <ComboBox fx:id="countries" layoutX="57.0" layoutY="171.0" prefHeight="37.0" prefWidth="150.0" promptText="SelectCountries" />
      <Button layoutX="191.0" layoutY="231.0" mnemonicParsing="false" onAction="#timeDisplay" prefHeight="37.0" prefWidth="80.0" text="Go" />

package application;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.ResourceBundle;
import java.util.TimeZone;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
public class TimeController implements Initializable {
	ComboBox<String> countries;
	ObservableList<String> list=FXCollections.observableArrayList("india","nepal","america","pakistan","srilanka","australia","china","russian","japan","france","bangladesh","iraq","united kingdom","Hong Kong");
	Label time_initialize=new Label();
	Label time_result=new Label();
     private static final String DATE_FORMAT = "dd-M-yyyy hh:mm:ss a";
	SimpleDateFormat formatter = new SimpleDateFormat(DATE_FORMAT);

	Date date1 = new Date(); 
	String dateInString = formatter.format(date1)+""; 
    Date date;
    TimeZone tz = TimeZone.getDefault();
	public void initialize(URL arg0, ResourceBundle arg1) {
		try {
			date = formatter.parse(dateInString);
		} catch (ParseException e) {

	public void timeDisplay(){
		String selectTimeZone=countries.getValue();
		String timeZone=getSelectTimeZone(selectTimeZone);
		SimpleDateFormat sdfAmerica = new SimpleDateFormat(DATE_FORMAT);
        TimeZone tzInAmerica = TimeZone.getTimeZone(timeZone);
        String sDateInAmerica = sdfAmerica.format(date);
        Date dateInAmerica = null;
		try {
			dateInAmerica = formatter.parse(sDateInAmerica);
		} catch (ParseException e) {
	private String getSelectTimeZone(String selectTimeZone) {
		switch (selectTimeZone) {
		case "india":	
			return "Asia/Kolkata";
		case "nepal":	
			return "Asia/Kathmandu";
		case "america":	
			return "America/New_York";
		case "pakistan":	
			return "Asia/Karachi";
		case "srilanka":	
			return "Asia/Colombo";
		case "australia":	
			return "Europe/Vienna";
		case "china":	
			return "Asia/Shanghai";
		case "russian":	
			return "Europe/Moscow";
		case "japan":	       
			return "Asia/Tokyo";
		case "france":	
			return "Europe/Paris";
		case "bangladesh":	
			return "Asia/Dhaka";
		case "iraq":	
			return "Asia/Baghdad";
		case "united kingdom":	
			return "Europe/London";
		case "Hong Kong":	
			return "Asia/Hong_Kong";
		return "Asia/Kolkata";
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;

public class Main extends Application {
	public void start(Stage primaryStage) {
		try {
			Parent root =FXMLLoader.load(getClass().getResource("/application/Time.fxml"));
			Scene scene = new Scene(root);
		} catch(Exception e) {
	public static void main(String[] args) {

26 views0 comments

Recent Posts

See All
bottom of page