小编Jas*_*cht的帖子

JRE是否支持posix TZ描述而不是TZ名称?

当OS使用POSIX时区描述而不是时区名称时,Java似乎没有应用DST偏移量。JRE不支持使用TZ描述吗?或者这是一个错误吗?

更多细节...

我正在基于Linux(Debian)的系统上工作,其中TZ环境变量设置为POSIX格式的TZ,STD+7DST+6,M3.2.0/02:00:00,M11.1.0/02:00:00而不是诸如的TZ名称America/Denver。(请参阅TZ变量

尽管这对于date相关的系统工具似乎正常工作,但是当我尝试在Java应用程序中查找时间时,似乎并未针对DST进行正确调整。导致夏令时生效的一年中的部分时间错误。

我已经在几个不同的系统上进行了测试,并在每个系统上看到了相同的结果(测试结果如下)

这种现象实际上在使用Quartz Scheduler的应用程序中浮出水面,但是我已经能够使用以下SSCCE重现该问题:

import java.util.Date;
import java.util.TimeZone;

public class CheckTime {
    public static void main(String[] args) {
        TimeZone tz = TimeZone.getDefault();
        Date now = new Date();
        System.out.println("Current time " + now.toString());
        System.out.println("Current time zone " + tz.getDisplayName());
        System.out.println("Current time zone in DST? " + tz.inDaylightTime(now));
    }
}
Run Code Online (Sandbox Code Playgroud)

Ubuntu 18.04.2 LTS(仿生)

$ java -version
openjdk version "1.8.0_222"
OpenJDK Runtime Environment (build …
Run Code Online (Sandbox Code Playgroud)

java linux datetime

7
推荐指数
1
解决办法
110
查看次数

在线程的情况下Thread.sleep和wait()方法的异常处理

我试图编写一个Producer Consumer模型(java中的Producer线程和Consumer Thread)

我想知道如何处理方法和类的方法InterruptedException引发的Thread.sleepObjectwait()

package producerconsumer;

import java.util.ArrayList;

public class Consumer implements Runnable {

    ArrayList<Integer> container;

    @Override
    public void run() {
        while(true){
        System.out.println("Consumer Thread Running");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if(null==container){
            System.out.println("container is null");
        }
        //Removing values from Arraylist
        synchronized (container) {
            if(container.size()==0){
                try {
                    container.wait();
                } catch (InterruptedException e) {
                    //How to tackle this exception
                    e.printStackTrace();
                }
            }
            Integer removedInteger = container.remove(container.size()-1);
            System.out.println("removedInteger..."+removedInteger);
        }
        }
    }

    public void setContainer(ArrayList<Integer> …
Run Code Online (Sandbox Code Playgroud)

java multithreading exception-handling

6
推荐指数
1
解决办法
1万
查看次数

如何将组件添加到JDialog

   d1=new JDialog();
   d1.setSize(200, 100);
   t1=new JTextField();
   t1.setBounds(10,10,40,20);
   d1.add(t1);
Run Code Online (Sandbox Code Playgroud)

我想在JDialog中添加组件,例如TextField,Button ......

java swing jdialog layout-manager

4
推荐指数
1
解决办法
3万
查看次数

如何解压缩zip文件的特定目录中的所有文件?

假设我有一个名为Bundles.zip的.zip文件,并且直接在Bundles.zip中,有几个文件和几个文件夹.这就是.zip的样子:

Bundles.zip顶级截图

现在,我想提取一切捆绑文件夹.我的程序已经知道从中提取文件所需的文件夹的名称,在本例中为Bundles.

zip中的Bundles文件夹可以包含子文件夹中的文件,子文件夹,文件,基本上都是这样的:

Bundles.zip内部文件夹截图

我只需要从Bundles文件夹中提取所有内容到输出目录.

我怎样才能在Java中实现这一目标?我找到了提取zip中所有文件和文件夹的答案,但我只需要提取zip中的特定文件夹,而不是一切.

到目前为止工作代码:

            ZipFile zipFile = new ZipFile(mapsDirectory + "mapUpload.tmp");
            Enumeration zipEntries = zipFile.entries();
            String fname;
            String folderToExtract = "";
            String originalFileNameNoExtension = originalFileName.replace(".zip", "");

            while (zipEntries.hasMoreElements()) {
                ZipEntry ze = ((ZipEntry)zipEntries.nextElement());

                fname = ze.getName();

                if (ze.isDirectory()) //if it is a folder
                {

                    if(originalFileNameNoExtension.contains(fname)) //if this is the folder that I am searching for
                    {
                        folderToExtract = fname; //the name of the folder …
Run Code Online (Sandbox Code Playgroud)

java zip extract unzip subdirectory

3
推荐指数
2
解决办法
7472
查看次数

Java/Swing - 如何减小JDialog构造函数的大小?

我正在用Java构建一个图形用户界面,我想知道是否有人可以给我一些指示.具体来说,当点击这个GUI中的一个按钮时,一个大的JDialog(它是一个扩展JDialog的类)打开,供用户输入一些信息,查看一些信息,点击一些东西等.

该类包含多个JLabel和其他组件.我的问题是如何创建这样的实例?现在我在构造函数中拥有所有内容,使其成为500(!)行构造函数.必须有一个替代品!构造函数大约有300行代码放置和设置代码,另外200行代表听众提供或接受.

另外一个问题是,现在我通过调用从另一个类打开这个窗口

MyClassExtendsJDIalog temp = new MyClassExtendsJDIalog();

但是我在创建它的类中根本不使用这个"temp"变量,因为"temp"的构造函数可以做到一切.看起来我再次以错误的方式做事.

感谢您阅读本文.

java user-interface swing constructor

1
推荐指数
1
解决办法
385
查看次数

无法将主类的字符串值设置为setter

public class StudentTest{

  public static void main(String args[]){

    UnderGrad uG1 = new UnderGrad();
    uG1.setName("John");
    uG1.setMatric("0192345");

    System.out.println("Undergarduate Student Info");
    uG1.setCourse(new Course("CS1103",3));
    uG1.setCourse(new Course("IT4504",3));
    uG1.displayStudentInfo();

    System.out.println(" ");

    PostGrad pG1 = new PostGrad();
    pG1.setName("Sam");
    pG1.setMatric("G015466");
    pG1.setResearch("Empirical Software Engineering");
    pG1.setResearch("Data Mining");
    System.out.println("Postgrad Student Info");
    pG1.displayStudentInfo();
  }
}

public class Course{
  private String courseName;
  private int crhour;

  public Course(String n, int c){
    courseName = n;
    crhour = c;
  }

  public void setCourseName(String course){
    courseName = course;
  }

  public String getCourseName(){
    return courseName;
  }

  public void …
Run Code Online (Sandbox Code Playgroud)

java arrays

0
推荐指数
1
解决办法
94
查看次数