Java assignment 11: Current date & time worldwide

  Quizzes     Assignments     Puzzles     Easy exercises     Required knowledge  
Java Assignments   Green = Easy,  Blue = Normal,  Red = Hard
Select    01   02   03   04   05   06   07   08   09   10   By title 

Write a program that allows users to enter a letter that represents a city in the world.
When the user inserts the letter and presses the enter button, the program writes the current date and time in that city.

In the following example the user entered the letter ā€œnā€, therefore the program shows the date and time in New York. If the the user inserts other letters than the program suggests, the program writes the local date and time as shown below.

a: Los_Angeles
n: New York
l: London
m: Moscow
k: Kolkata
s: Shanghai
t: Tokyo:

Enter the letter of the country: n
New York: 23.03.2019 05:53:39

There are different methods to write this program, but here is a code that helps you to grasp the idea.

Java assignment 11: Current date & time worldwide

Level: Normal

public class WorldClock
{
  public void getNow(char cityLetter)
  {
    Date now = new Date();
    DateFormat df = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
    switch(cityLetter)
    {
      // add your code in the cases
      case 'a':
        // Current date & time in Los Angeles
      case 'n':
        // Current date & time in New York
      case 'l':
        // Current date & time in London
      case 'm':
        // Current date & time in Moscow
      case 'k':
        // Current date & time Kolkata
      case 's':
        // Current date & time in Shanghai
      case 't':
        // Current date & time in Tokyo
      default:
        // Current local date & time local
    }
  }
  public static void main(String[] args)
  {
    WorldClock wcl = new WorldClock();
    Scanner input = new Scanner(System.in);
    // add your code here
  }
}
Author: Sar Maroof

Answer explanation

The following code writes the date and time of the cities Los Angeles, New York, London, Moscow, Kolkata, Shanghai and Tokyo.

 

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
import java.util.TimeZone;

public class WorldClock
{
  public void getNow(char cityLetter)
  {
    Date now = new Date();
    DateFormat df = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
    switch(cityLetter)
    {
      case 'a':
        // Current date & time in Los Angeles
        df.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));
        String dLosAngeles = df.format(now);
        System.out.println("Los Angeles: " + dLosAngeles);
        break;
      case 'n':
        // Current date & time in New York
        df.setTimeZone(TimeZone.getTimeZone("America/New_York"));
        String dNewYork = df.format(now);
        System.out.println("New York:    " + dNewYork);
        break;
      case 'l':
        // Current date & time in London
        df.setTimeZone(TimeZone.getTimeZone(" Europe/London"));
        String dLondon = df.format(now);
        System.out.println("London:      " + dLondon);
        break;
      case 'm':
        // Current date & time in Moscow
        df.setTimeZone(TimeZone.getTimeZone("Europe/Moscow"));
        String dMoscow = df.format(now);
        System.out.println("Moscow:      " + dMoscow);
        break;
      case 'k':
        // Current date & time Kolkata
        df.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
        String dKolkata = df.format(now);
        System.out.println("Kolkata:     " + dKolkata);
        break;
      case 's':
        // Current date & time in Shanghai
        df.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        String dShanghai = df.format(now);
        System.out.println("Shanghai:    " + dShanghai);
        break;
      case 't':
        // Current date & time in Tokyo
        df.setTimeZone(TimeZone.getTimeZone("Asia/Tokyo"));
        String dTokyo = df.format(now);
        System.out.println("Tokyo:       " + dTokyo);
        break;
      default:
        // Current local date & time local
        String localDateTime = df.format(now);
        System.out.println("Local:      " + localDateTime);
    }
  }
  public static void main(String[] args)
  {
    WorldClock wcl = new WorldClock();
    Scanner input = new Scanner(System.in);
    System.out.println(
        "a: Los_Angeles \n"
            + "n: New York \n"
            + "l: London \n"
            + "m: Moscow \n"
            + "k: Kolkata \n"
            + "s: Shanghai \n"
            + "t: Tokyo:\n");
    System.out.print("Enter the letter of the country: ");
    char cityLetter = input.next().charAt(0);
    wcl.getNow(cityLetter);
    input.close();
  }
}
/*
a: Los_Angeles 
n: New York 
l: London 
m: Moscow 
k: Kolkata 
s: Shanghai 
t: Tokyo:

Enter the letter of the country: n
New York:    23.03.2019 09:49:49
*/

The second edition of Learn Java and Master Writing Code is easy to understand, organized to learn Java voor beginners and it guides readers to master Java code by solving more than 100 exercises and assignments. This book explains each chapter properly before starting with the exercises and the assignments. Available on Amazon. See inside the book here!


Sar Maroof is a professional software development teacher, gives master classes and publishes technical articles. He is also an expert software developer and worked for several big as well as small companies and later as a freelancer.
 Subscribe to Sar Maroof's RSS feeds!
 Mail this page to a friend!
 Bookmark this page!

Leave a Reply

Your email address will not be published. Required fields are marked *