Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Complete the program that shows the current time in a specified city at the URL https://www.timeanddate.com/worldclock/usa/ city-name (where city-name is the name of the city

Complete the program that shows the current time in a specified city at the URLhttps://www.timeanddate.com/worldclock/usa/city-name (where city-name is the name of the city allowed by the size). The URL returns a page with information about the current time. Ask the user for the city in the United States and append the city to the URL (in place of the string city-name. City names such as denver, chicago, las-vegas, miami, and new-york can be used. Sends the request to the server using the URLConnection. Get the location, time, and date from the HTML in the web page. Print the location, the time and the date in that city.

Use the provided GetTimeInCity.java code as a starting point. Fill in the code to create the urlString as shown above, including the city. The code to create a URL from the urlString, open the URLConnection from the URL object, and get the HttpURLConnection from the URLConnection object is provided. Then, complete the code in that finds the tag

, gets the city information, finds the tag , gets the time following the tag, and finds the tag and gets the date.

This is some of the HTML code that is retrieved when we access the time page on the website (I have added line separators to make the HTML more legible): . . .

image text in transcribed Current Local Time in Chicago, Illinois, USA

5:46:22 pm CDT

Sunday, March 26, 2017

Country: United States

State: Illinois (IL)

Lat/Long: 4153'N / 8738'W

. . . A sample run of the program:

Please enter the city name: miami

Where: Current Local Time in Miami, Florida, USA

Time: 7:20:23 pm

Date: Sunday, March 26, 2017

An HTML parsing library, jsoup.jar, has been included to ease parsing the HTML document sent by the server. Be sure to add it to your Projects Properties Build Path Libraries.

If your program works correctly, it will only output the city information, time and date for the selected city.

GetTimeInCity.java:

import java.util.Scanner; import java.io.IOException; import java.net.URL; import java.net.URLConnection; import java.net.HttpURLConnection; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.select.Elements; /** * This class shows the selected city, time and date in the specified city. * @author your-name * */ public class GetTimeInCity { public static void main(String[] args) throws IOException { Scanner console = new Scanner(System.in); System.out.print("Please enter the city name: "); String city = console.next(); // TODO: Build the URL string. Note that the city *must* be added as a parameter to the URL! // Open a URLConnection using the URL. String urlstring = ... // This uses a URL to open the URLConnection. URL url = new URL(urlstring); URLConnection uc = url.openConnection(); HttpURLConnection hc = (HttpURLConnection) uc; // Check to see if the request was successful. if (hc.getResponseCode() != 200) { System.err.println("Error: HTTP server response code is " + hc.getResponseCode() + " " + hc.getResponseMessage()); return; } // Make sure we received HTML text data. if (!hc.getContentType().startsWith("text/html")) { System.err.println("Error: data received from " + urlstring + " is not text/html, but " + hc.getContentType()); return; } // Use JSoup to parse the HTML into objects, starting at // the Document level. Document doc = Jsoup.parse(hc.getInputStream(), null, hc.getURL().toString()); // TODO: Get the city information. Use JSoup's doc.select() // method to find the element chain: header > h1 Elements h1 = ... // Get the text. String foundCity = h1.text(); // TODO: Use JSoup's doc.select() to find the time from // the  tag in the document. Elements span_ct = ... // Get the text. String time = span_ct.text(); // TODO: Use JSoup's doc.select() to find the date in // the  tag. Elements span_ctdat = ... // Get the text. String date = span_ctdat.text(); System.out.println("Where: " + foundCity); System.out.println("Time: " + time); System.out.println("Date: " + date); } } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Explain three broad global HR challenges.

Answered: 1 week ago