I have two lines that I want to print on my Main App Screen. I tried to set them equal to a variable to print on the main screen. What I put into the Java file was:

public class TimeZoneConverter {

            public void main(String args[]) {

             //Date will return local time in Java  
             Date localTime = new Date(); 

             //creating DateFormat for converting time from local timezone to GMT
             SimpleDateFormat converter = new SimpleDateFormat("dd/MM/yyyy:HH:mm:ss");

             //getting GMT timezone, you can get any timezone e.g. UTC
             converter.setTimeZone(TimeZone.getTimeZone("GMT"));

             String1 = System.out.println("local time : " + localTime);;
             String2 = System.out.println("time in GMT : " + converter.format(localTime));

            }
        }

}

It says the strings cannot be resolved to a variable. How would I try to get this onto my main screen?

有帮助吗?

解决方案

I tried to set them equal to a variable to print on the main screen.

This is how you extract them to variables.

String localTimeStr = "local time : " + localTime;
String gmtStr = "time in GMT : " + converter.format(localTime);
System.out.println(localTimeStr);
System.out.println(gmtStr);

System.out.println() returns a void. You cannot assign that to anything.

[EDIT]

Since you're looking to print this on an Android screen use a Toast or find a text view and set it's text.

其他提示

It says the strings cannot be resolved to a variable.

That's because you didn't declare them! Specifically, you are trying to assign to variables String1 and String2 which were not declared, and therefore are not recognized by the Java compiler.

But even if you did declare them, the code still wouldn't work. The reason is that println is a void method; i.e. it doesn't return anything. And the Java language won't let you assign "nothing" to a variable ... 'cos it doesn't make sense.

See @Reimus's answer for the right way to write those statements.

First of all Syste.out.println() does not return anything. It is a void. So you have to say like this:

 System.out.println("local time : " + localTime);
 System.out.println("time in GMT : " + converter.format(localTime
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top