简体   繁体   English

编程中的Out.println()

[英]Out.println() in programming

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: 我放入Java文件的内容是:

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. System.out.println()返回一个空值。 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 . 由于您要在Android屏幕上进行打印,因此请使用Toast或查找文本视图并将其设置为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. 具体来说,您要分配给变量String1String2其中没有申报,因此不被Java编译器识别。

But even if you did declare them, the code still wouldn't work. 但是,即使您确实声明了它们,代码仍然无法正常工作。 The reason is that println is a void method; 原因是printlnvoid方法。 ie it doesn't return anything. 即它不返回任何东西。 And the Java language won't let you assign "nothing" to a variable ... 'cos it doesn't make sense. 而且Java语言不允许您为变量分配“无”……因为这没有意义。

See @Reimus's answer for the right way to write those statements. 有关编写这些语句的正确方法,请参见@Reimus的答案。

First of all Syste.out.println() does not return anything. 首先, Syste.out.println()不返回任何内容。 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 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM