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");

Time.fxml
<?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="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.TimeController">
   <children>
      <Label layoutX="147.0" layoutY="29.0" prefHeight="46.0" prefWidth="190.0" text="Time Conversion">
         <font>
            <Font size="25.0" />
         </font>
      </Label>
      <Label layoutX="69.0" layoutY="95.0" prefHeight="37.0" prefWidth="86.0" text="Indian Time">
         <font>
            <Font size="15.0" />
         </font>
      </Label>
      <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" />
   </children>
</AnchorPane>


TimeController,java
package application;

import java.net.URL;
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 {
	@FXML
	ComboBox<String> countries;
	ObservableList<String> list=FXCollections.observableArrayList("india","nepal","america","pakistan","srilanka","australia","china","russian","japan","france","bangladesh","iraq","united kingdom","Hong Kong");
	
	@FXML
	Label time_initialize=new Label();
	
	@FXML
	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();
	
	@Override
	public void initialize(URL arg0, ResourceBundle arg1) {
		countries.setItems(list);
		try {
			date = formatter.parse(dateInString);
		} catch (ParseException e) {
			e.printStackTrace();
		}

	    time_initialize.setText(formatter.format(date1));
	}
	public void timeDisplay(){
		String selectTimeZone=countries.getValue();
		String timeZone=getSelectTimeZone(selectTimeZone);
		SimpleDateFormat sdfAmerica = new SimpleDateFormat(DATE_FORMAT);
        TimeZone tzInAmerica = TimeZone.getTimeZone(timeZone);
        sdfAmerica.setTimeZone(tzInAmerica);
        String sDateInAmerica = sdfAmerica.format(date);
        Date dateInAmerica = null;
		try {
			dateInAmerica = formatter.parse(sDateInAmerica);
		} catch (ParseException e) {
			e.printStackTrace();
		}
        time_result.setText(formatter.format(dateInAmerica)+"");
	}
	
	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";
		default:
			break;
		}
		return "Asia/Kolkata";
	}
}

Main.java
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 {
	@Override
	public void start(Stage primaryStage) {
		try {
			Parent root =FXMLLoader.load(getClass().getResource("/application/Time.fxml"));
			Scene scene = new Scene(root);
			scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
			primaryStage.setScene(scene);
			primaryStage.show();
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		launch(args);
	}
}


1 comment

Contact Us

Tel: (+91) 0120  4118730  

Time :   10 : 00  AM -  08 : 00 PM IST 

Registered address: G-69, Sector 63, 

 Noida - 201301, India

We Provide Services Across The different countries

USA    Australia   Canada   UK    UAE    Singapore   New Zealand    Malasia   India   Ireland   Germany

CodersArts is a Product by Sofstack Technology Solutions Pvt. Ltd.

  • CodersArts | Linkedin
  • Instagram