当我启动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
请帮忙.非常感谢!
我正在研究我的第一个简单的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) 无法使用@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)
环境
以下是一个简单的工作程序,它使用两个线程来打印计数器:
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)