Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
导入Java日期类_Java_Eclipse_Date_Import - Fatal编程技术网

导入Java日期类

导入Java日期类,java,eclipse,date,import,Java,Eclipse,Date,Import,我不得不编辑原始问题,因为我认为许多作者误解了它 我有最基本的问题。尝试获取系统日期。我的代码是这个 import java.util.Date; public class DateDemo { public static void main(String args[]) { // Instantiate a Date object Date date = new Date(); // display time and date using toS

我不得不编辑原始问题,因为我认为许多作者误解了它


我有最基本的问题。尝试获取系统日期。我的代码是这个

import java.util.Date;

public class DateDemo {
   public static void main(String args[]) {
      // Instantiate a Date object
      Date date = new Date();

      // display time and date using toString()
      System.out.println(date.toString());
   }
}
它给了我一个错误:标记“import”上的语法错误,应该是assert

解决方案应该非常基本,但我找不到


谢谢你的帮助。

你试图做一些不可能的事。您应该创建一个类,并在该类的方法中执行这些操作。在下面的示例中,您在类的主方法中执行这些操作。主方法是程序开始的地方

除此之外,如果类位于包中,则应在类的最开始处定义它。例如,您的包被称为
myPackage
,您应该像下面那样将其添加到代码中

例如,在包
myPackage
中创建类
Foo

package myPackage;
import java.util.Date;

public class Foo {
   public static void main(String args[]) {

      // You create a new date object and assign it to the sysdate
      Date sysdate = new Date();

      // You are now displaying your time and date using toString()
      System.out.println(sysdate.toString());
   }
}

Java中的指令不能在类之外。那么你的指示呢

Date sysdate = new Date();
System.out.println(sysdate);
应该在方法中(例如,在
main
中),并且该方法必须在类中声明。据我所知,
import
package
指令是类定义之前唯一需要的指令


还请注意,编写类的文件必须与类同名,例如,示例
DateDemo
必须保存在名为
DateDemo.java

的文件中。关于您没有将正确的类结构包含在main方法中,其他答案是正确的

切勿使用日期 另外,永远不要使用该类。它的设计非常糟糕,而且相当混乱和麻烦。现在是遗留的,被
java.time.Instant
类取代

java.time 若要表示中的某个时刻,请使用。该类使用的分辨率与。当前时刻可能仅通过或取决于主机、主机和实现的限制来捕获

示例代码

package com.basilbourque.example.telltimeapp ;

import java.time.Instant ;

public class TellTime {
    public static void main( String args[] ) 
    {
       Instant instant = Instant.now() ;     // Capture the current moment in UTC. 
       String output = instant.toString() ;  // Generate text in a `String` object representing the value of the `Instant` object in standard ISO 8601 format. 
       System.out.println( output ) ;        // Dump text to the console.                  
    }
}
让我们变得更现实。在中启动我们的应用程序。然后在该应用程序对象上调用一个方法来启动

我们的应用程序知道如何做两件事:

  • 用英语说出当前时间
  • 在中告诉当前时间
我们的
main
方法要求这两种方法中的第一种

package com.basilbourque.example.telltimeapp ;

import java.time.Instant ;

public class TellTime {
    public static void main( String args[] )  // The `main` method is *not* object-oriented, just a hack, the way we solve the chicken-or-the-egg dilemma] of how to launch an OOP app.
    {
        TellTime app = new TellTime() ;       // Create an instance of our app, an object that represents the entire running app.
        app.tellCurrentMomentInUtc() ;        // Ask our app object to run some code.
    }

    public void tellCurrentMomentInUtc() 
    {
        Instant instant = Instant.now() ;     // Capture the current moment in UTC. 
        String output = instant.toString() ;  // Generate text in a `String` object representing the value of the `Instant` object in standard ISO 8601 format. 
        System.out.println( output ) ;        // Dump text to the console. 2018-07-02T20:17:49.929677Z
    }

    public void tellCurrentMomentInMontreal() 
    {
        Instant instant = Instant.now() ;             // Capture the current moment in UTC. 
        ZoneId z = ZoneId.of( "America/Montreal" ) ;  // A time zone is a history of the past, present, and future changes to the offset-from-UTC used by the people of a particular region.
        ZonedDateTime zdt = instant.atZone( z ) ;     // Adjust from UTC to Québec time. Same moment, same point on the timeline, different wall-clock time.
        DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.FULL ).withLocale( Locale.CANADA_FRENCH ) ;   // Be aware that time zone and locale have *nothing* to do with one another, completely orthogonal issues.
        String output = zdt.format( f ) ;             // Generate text in a localized format.
        System.out.println( output ) ;                // Dump text to the console. Example: lundi 2 juillet 2018 à 16:16:59 Eastern Daylight Time
    }

}
教程 在过帐到堆栈溢出之前,请研究Oracle提供的


关于java.time 该框架内置于Java8及更高版本中。这些类取代了麻烦的旧日期时间类,例如,&

该项目现已启动,建议迁移到类

要了解更多信息,请参阅。并搜索堆栈溢出以获得许多示例和解释。规格是

您可以直接与数据库交换java.time对象。使用兼容的或更高版本。不需要字符串,也不需要
java.sql.*

从哪里获得java.time类

  • 然后
    • 内置的
    • 标准JavaAPI的一部分,带有捆绑实现
    • Java9添加了一些次要功能和修复
    • 大部分java.time功能都在中向后移植到Java6和Java7
    • 更高版本的Android捆绑包实现了java.time类

    • 对于早期的Android(在你开始做这些事情之前,请更多地关注Java的基础知识。谷歌的“Java软件包声明”阅读。我投票结束这个问题,因为作者还没有学会“Hello World”Java编程的最基本的基础知识(类和
      main
      method)。顺便说一句,完成这个日期时间工作的现代方法是:
      java.time.Instant.now().toString()
      谢谢你的帮助,我发现我的代码在文字上很愚蠢,但当我尝试你的代码时,它还是给了我同样的错误。因为我是一个乞丐,我不知道它出了什么问题。我们都犯了同样的错误。一点也不愚蠢。Sen canınısıkma;)你能把整个程序复制到这里吗?整个程序就是这样,正如我所说的,我是一个乞丐。我只是在学习java的基础知识。正如一些作者之前提到的,我只学习java的基础知识,我复制了上面的代码,它在我的本地机器上运行。你可以写出整个错误。有了这一点,我可以帮得更好。我已经编辑了我的答案。请注意上面的软件包信息。谢谢,我根据你写的修改了代码,但仍然有同样的问题。上面的代码符合您提到的所有内容。该文件名为DateDemo.java,指令位于类中声明的方法中。但是错误是它期望“断言”,我不知道它是什么。