Display city and time by Longitude in JAVA

In summary: It looks like you're making some progress, but you're also making things more complicated than they have to be. You don't need to keep track of each city's time zone, and you don't need to reverse the time by 6 hours. Just figure out how many hours to advance or reverse based on the latitude, and the Worldclock class will do the rest.
  • #1
Albert12
9
0
Java Help!

I have Duty for my Jave class, My teacher needs to disply city and time by Longitude. which I set up to GMT but I have trouble problem. code seems to me work fine, I can't figure out wht's problems. it will be nice to get from you help.


Code:
package worldclockdriver;

import java.util.Date;

public class WorldClockDriver {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Clock[] GMTClocks = new Clock[5]; // Instantiate an array that will contain 5 Clock objects

for(int i=0; i<5; i++){
Clock MyClock = new Clock("Thursday", 0 ,0); // Instantiate a clock object: Thursday 12:00 am
Date Now = new Date(); // Instantiate a Date object, contains current time in GMT time zone
long milli = Now.getTime(); // Obtain current time in Greenwich (GMT) in milliseconds elapsed since Thursday, Jan. 1, 1970
MyClock.advance(milli/1000/60); // Modify MyClock to reflect current hours and minutes in GMT time zone
GMTClocks[i] = MyClock; // populate array with GMT Clocks
}

WorldClock WC1 = new WorldClock("St. Louis", "90 W", GMTClocks[0]); // Instantiate a CST (St. Louis local time) clock
System.out.println(WC1);

WorldClock WC2 = new WorldClock("Beijing", "120 E", GMTClocks[1]); // Instantiate a Beijing local time clock
System.out.println(WC2);

WorldClock WC3 = new WorldClock("Bombay", "82 E", GMTClocks[2]); // Instantiate a Bombay ocal time (Indian Standard Time) clock
System.out.println(WC3);

WorldClock WC4 = new WorldClock("Moscow", "45 E", GMTClocks[3]); // Instantiate a Moscow local time clock
System.out.println(WC4);

WorldClock WC5 = new WorldClock("London", "0 W", GMTClocks[4]); // Instantiate a London local time (GMT) clock
System.out.println(WC5);

}
}

class WorldClock{
private String City;
private String Longitude;
private Clock TimeandDay;

public WorldClock(String s, String l, Clock c){
/* Insert code here. This constructor must initialize the object *
* with relevant values for City, Longitude and TimeandDay *
* Note: TimeandDay is a GMTClock object, which must be advanced *
* or reversed to reflect the current local time */

////// I just marked this code with blue because my I be I have problem that I don't know here:

[B][COLOR="Blue"]
City = s;
Longitude = l;
TimeandDay = c;

String []splits = l.split(" ");
long m = Integer.parseInt(splits[0]);
String t = splits[1];
m = m*4;[/COLOR][/B]}

public String toString(){
return(City + " (" + Longitude + ") " + TimeandDay.toString());
}
}//end of class Worldclock


class Clock{
/************************************************** *************
* Objects of this class represent time (in hours and minutes) *
* The time data is stored in 24 hour (HHMM military format) *
************************************************** *************/
private String City;
private int day_of_week; // valid range of values is 1 thru' 7
private long hours; // range of values is 0 thru' 23
private long minutes; // range of values is 0 thru' 59
private String[] dow = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};


public Clock(String weekday, int h, int m){
hours = h;
minutes = m;
day_of_week=1;
for (int i=0; i<dow.length; i++){
if (weekday.equals(dow[i])){
day_of_week = i+1;
break;
}
}
}

public void advance(long m){
final long WEEKMINUTES = 7*24*60;
final long DAYMINUTES = 24*60;
final long HOURMINUTES = 60;


long totalmin = (day_of_week>1? (day_of_week-1)*DAYMINUTES + hours*HOURMINUTES + minutes + m : hours*HOURMINUTES + minutes + m);
totalmin = (totalmin < 0? (totalmin%WEEKMINUTES)+WEEKMINUTES : totalmin);
day_of_week = (int)((totalmin/DAYMINUTES)>0? (totalmin/DAYMINUTES)+1 : 1);
day_of_week = (day_of_week>7? day_of_week%7 : day_of_week);
hours = (totalmin%DAYMINUTES)/HOURMINUTES;
minutes = (totalmin%DAYMINUTES)%HOURMINUTES;
}

public void reverse(long m){
advance(-1*m);
}


public String toString(){
String S = dow[day_of_week-1] + ", ";
S = S + (hours>12? hours-12 : hours<1? 12 : hours) + ":" + (minutes<10? ("0"+minutes) : minutes) + (hours>=12? " pm" : " am");
return S;
}

} // end of class Clock


The Attempt at a Solution



disply:
Code:
St. Louis (90 W) Friday, 1:47 am
Beijing (120 E) Friday, 1:47 am
Bombay (82 E) Friday, 1:47 am
Moscow (45 E) Friday, 1:47 am
London (0 W) Friday, 1:47 am
BUILD SUCCESSFUL (total time: 1 second)
 
Last edited:
Physics news on Phys.org
  • #2


Your WorldClock constructor doesn't seem to be working correctly. When this constructor is called, it is passed three parameters: a city name, the longitude, and a Clock instance. For every 15 degrees of latitude west of the Greenwich Meridian, the clock should be reversed one hour. For every 15 degrees of latitude east of the Greenwich Meridian, the clock should be advanced one hour. You need to have logic in your constructor to parse the latitude, figure out if it's west or east of Greenwich, and reverse or advance the clock accordingly. For example, St. Louis is 90 deg. west, so the time should be reversed by 6 hours. If it's 12:00 midnight in London, it will be 4:00pm the day before. For Bombay (Mumbai), the time would be 5:00am, I think.
 
  • #3


Was there a requirement in the assignment to not use a GregorianCalendar? It has time zone handling built in.
 
  • #4


no there is not any requirments, he said if you add any code you should understand it well, not just copy it.
 
  • #5


I just changed a little bit in my code and still dint give me the result that I need. I add a new arrry to class clack and I add if statement to chose "W" or "E" depent on the time.
Code:
String[] splits = {"90", "W","120", "E","82","E","45","E","0", "W"};

the other code I add :

Code:
City = s;
Longitude = l;
TimeandDay = c;



String []splits = Longitude.split(" ");


int m = Integer.parseInt(splits[0]);

int m1  = m*4;
String t = splits[1];


     if (t.endsWith("W")){

         m1=m1-m;
    }
     else {

     if (t.endsWith("E")){
    m1=m1+m;
    }

    }}
 
  • #6


If you're going to hardcode the cities, why would you put everything into a single array and then try to parse out the data that you need? What if the cities change? You need to step back and consider the time differences mathematically. Figure out the formula that you need to know for the time differences before you try to code it. Once you have that formula, the next step should be more apparent.
 
  • #7


I agree with Borg that the array with latitudes is not a good idea. As I said before, you need logic in your Worldclock constructor that takes the latitude (including the W or E suffix) and figures out how many hours to advance or reverse the Clock's time. Each 15 degrees W or E corresponds to one hour back or ahead.
 

1. How do I display the city and time by longitude using Java?

To display the city and time by longitude in Java, you can use the java.time.ZoneId class to get the time zone based on the longitude value. Then, you can use the java.time.ZonedDateTime class to get the current date and time in that time zone. Finally, you can use the java.time.format.DateTimeFormatter class to format the date and time according to your desired output format.

2. Can I display the city and time for a given longitude value in a different language?

Yes, you can display the city and time for a given longitude value in a different language by using the java.util.Locale class to specify the desired language and country. Then, you can use the java.time.format.DateTimeFormatter class with the specified locale to format the date and time in the desired language.

3. How do I handle time zone differences when displaying the city and time by longitude in Java?

To handle time zone differences when displaying the city and time by longitude in Java, you can use the java.time.ZoneOffset class to get the time zone offset based on the longitude value. Then, you can use the java.time.ZonedDateTime class to get the current date and time in the specified time zone offset. This will account for any differences in time zones between the longitude value and your current location.

4. Is it possible to display the city and time for multiple longitude values at once?

Yes, it is possible to display the city and time for multiple longitude values at once in Java. You can use a loop to iterate through a list of longitude values and use the same logic mentioned above to display the city and time for each longitude value.

5. Are there any external libraries or APIs that can help with displaying the city and time by longitude in Java?

Yes, there are some external libraries and APIs that can assist with displaying the city and time by longitude in Java. One example is the java.time.zone.TimeZoneNames class, which provides localized display names for time zones. Another example is the java.time.zone.ZoneRules class, which provides information about the time zone rules for a specific longitude value. Additionally, there are third-party APIs such as the Google Time Zone API that can provide time zone information for a given longitude value.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
9
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
Back
Top