Java Tutorial/JPA/Calendar Date
Содержание
Date Mapped To Timestamp By Default
File: Student.java
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Student {
@Id
private long id = System.currentTimeMillis();
@Basic
private String name;
@Basic
private Date dateOfBirth = new Date();
public Date getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(Date dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Java Calendar Mapped To Date
File: Helper.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
public class Helper {
public static void checkData() throws Exception {
Class.forName("org.hsqldb.jdbcDriver");
Connection conn = DriverManager.getConnection("jdbc:hsqldb:data/tutorial", "sa", "");
Statement st = conn.createStatement();
ResultSet mrs = conn.getMetaData().getTables(null, null, null, new String[] { "TABLE" });
while (mrs.next()) {
String tableName = mrs.getString(3);
System.out.println("\n\n\n\nTable Name: "+ tableName);
ResultSet rs = st.executeQuery("select * from " + tableName);
ResultSetMetaData metadata = rs.getMetaData();
while (rs.next()) {
System.out.println(" Row:");
for (int i = 0; i < metadata.getColumnCount(); i++) {
System.out.println(" Column Name: "+ metadata.getColumnLabel(i + 1)+ ", ");
System.out.println(" Column Type: "+ metadata.getColumnTypeName(i + 1)+ ": ");
Object value = rs.getObject(i + 1);
System.out.println(" Column Value: "+value+"\n");
}
}
}
}
}
Map Java Calendar To Timestamp
File: Student.java
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
public class Student {
@Id
private String id;
private String name;
@Temporal(TemporalType.TIMESTAMP)
private java.util.Calendar dob;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public java.util.Calendar getDob() {
return dob;
}
public void setDob(java.util.Calendar dob) {
this.dob = dob;
}
public String toString() {
return "\n\nID:" + id + "\nName:" + name + "\n\n"+"Dob"+dob;
}
}
Map Java Date To Date
File: Student.java
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
public class Student {
@Id
private String id;
private String name;
@Temporal(TemporalType.DATE)
private java.util.Date dob;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public java.util.Date getDob() {
return dob;
}
public void setDob(java.util.Date dob) {
this.dob = dob;
}
public String toString() {
return "\n\nID:" + id + "\nName:" + name + "\n\n"+"Dob"+dob;
}
}
Map Java Date To Time
File: Student.java
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
public class Student {
@Id
private String id;
private String name;
@Temporal(TemporalType.TIME)
private java.util.Date dob;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public java.util.Date getDob() {
return dob;
}
public void setDob(java.util.Date dob) {
this.dob = dob;
}
public String toString() {
return "\n\nID:" + id + "\nName:" + name + "\n\n"+"Dob"+dob;
}
}
Map Java Date To Timestamp
File: Student.java
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
public class Student {
@Id
private String id;
private String name;
@Temporal(TemporalType.TIMESTAMP)
private java.util.Date dob;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public java.util.Date getDob() {
return dob;
}
public void setDob(java.util.Date dob) {
this.dob = dob;
}
public String toString() {
return "\n\nID:" + id + "\nName:" + name + "\n\n"+"Dob"+dob;
}
}
Mark Java Calendar With Temporal Type Date
File: Student.java
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
public class Student {
@Id
private String id;
private String name;
@Temporal(TemporalType.DATE)
private java.util.Calendar dob;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public java.util.Calendar getDob() {
return dob;
}
public void setDob(java.util.Calendar dob) {
this.dob = dob;
}
public String toString() {
return "\n\nID:" + id + "\nName:" + name + "\n\n"+"Dob"+dob;
}
}
Temporal Type DATE
File: Student.java
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
public class Student {
@Id
private String id;
private String name;
@Temporal(TemporalType.DATE)
private java.util.Calendar dob;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public java.util.Calendar getDob() {
return dob;
}
public void setDob(java.util.Calendar dob) {
this.dob = dob;
}
public String toString() {
return "\n\nID:" + id + "\nName:" + name + "\n\n"+"Dob"+dob;
}
}
Temporal Type TIMESTAMP
File: Student.java
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
public class Student {
@Id
private String id;
private String name;
@Temporal(TemporalType.TIMESTAMP)
private java.util.Calendar dob;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public java.util.Calendar getDob() {
return dob;
}
public void setDob(java.util.Calendar dob) {
this.dob = dob;
}
public String toString() {
return "\n\nID:" + id + "\nName:" + name + "\n\n"+"Dob"+dob;
}
}