문제

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