小编use*_*717的帖子

Java System.getProperty("user.timezone")不起作用

当我启动java程序时java -Duser.timezone="UTC",

System.out.println( System.getProperty( "user.timezone" ) );
System.out.println( new Date() ); // prints time in UTC 
Run Code Online (Sandbox Code Playgroud)

打印UTC时间,但是当我在代码中设置如下:

System.setProperty( "user.timezone", "UTC" );
System.out.println( System.getProperty( "user.timezone" ) );  // prints 'UTC'
System.out.println( new Date() ); // prints time in local zone, not in UTC
Run Code Online (Sandbox Code Playgroud)

不以UTC格式打印时间.我需要在代码中设置时间.不寻找乔达

环境:JDK 1.6/Windows XP

请帮忙.非常感谢!

java timezone datetime properties system

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

具有值'create'的Hibernate的'hbm2ddl.auto'属性不会重新创建表

我正在研究我的第一个简单的Hibernate应用程序.问题的症结在于,我已经重命名了持久化类的成员(对所有其他部分进行了适当的更改)并重新运行应用程序.由于'hbm2ddl.auto'属性在配置xml中被赋予'create'值,所以Hibernate应该在每次运行时创建新表,但是,它没有这样做.以下是详细信息:

类:

    public class Event {
    private Long id;

    private String title;
    private Date date;

    public Event() {
        // this form used by Hibernate
    }

    public Event(String title, Date date) {
        // for application use, to create new events
        this.title = title;
        this.date = date;
    }

    public Long getId() {
        return id;
    }

    private void setId(Long id) {
        this.id = id;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    } …
Run Code Online (Sandbox Code Playgroud)

hibernate hbm2ddl

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

JPA - 使用转换器保留 @Temporal LocalDate

无法使用@Temporalfor编译代码LocalDate

实体代码

...
@Temporal(TemporalType.DATE)
private LocalDate creationDate;

public LocalDate getCreationDate() {
    return this.creationDate;
}

public void setCreationDate(LocalDate creationDate) {
    this.creationDate = creationDate;
}
...
Run Code Online (Sandbox Code Playgroud)

转换器代码

@Converter(autoApply=true)
public class DateConverter implements AttributeConverter<LocalDate, Date> {

    @Override
    public Date convertToDatabaseColumn(LocalDate localDate) {
        return (localDate == null) ? null : Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
    }

    @Override
    public LocalDate convertToEntityAttribute(Date date) {
        return (date==null) ? null : Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDate();
    }

}
Run Code Online (Sandbox Code Playgroud)

持久化文件

...
<persistence-unit name="HR">
    <class>test.Employee</class>
    <class>test.DateConverter</class>
</persistence-unit>
...
Run Code Online (Sandbox Code Playgroud)

环境

  1. JDK 8
  2. Eclipse JEE - …

hibernate oracle11g java-8 jpa-2.1

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

应该何时声明方法同步?

以下是一个简单的工作程序,它使用两个线程来打印计数器:

public class SynchronizedCounter implements Runnable {

    private static int i = 0;

    public void increment() { i++; }
    public int getValue() { return i; }  

    @Override
    public void run() {
        for( int i = 0; i < 5; i++ ) {
            synchronized( this ) {
                increment();
                System.out.println( Thread.currentThread().getName() + " counter: " + this.getValue() );
            }
        }
    }

    public static void main( String[] args ) {

        ExecutorService executorService = Executors.newFixedThreadPool( 2 );
        SynchronizedCounter synchronizedCounter = new SynchronizedCounter();
        executorService.submit( synchronizedCounter …
Run Code Online (Sandbox Code Playgroud)

java concurrency multithreading thread-synchronization

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