使用 java 在 JPA 中以 AM/PM 形式插入和获取时间查询

insert and get Query for Time as AM/PM in JPA using java

This is the question

SELECT TIME_FORMAT( `locStart`, '%h:%i %p' ) FROM locationtimes.

我如何使用 JPA 实现它

这些是我的数据库 Table 个条目

我的 java 实体 class

package models;

package models;

import util.MyConverter;

import javax.persistence.*;
import java.sql.Time;
import java.sql.Timestamp;

/**
 * Created by abhinav on 11/2/15.
 */

@Entity
@Table(name = "locationtimes")
//@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id", scope = Locationtimes.class)
public class Locationtimes {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Long id;

    @Column
    public Long locationId;

    @Column
    public String weekday;

    @Column(insertable = false, updatable = false)
    @Temporal(TemporalType.TIME)
    public Time locStart;

    @Column(insertable = false, updatable = false)
    public Time locEnd;

    @Column(insertable = false, updatable = false)
    public Time todayStart;

    @Column(insertable = false, updatable = false)
    public Time futureStart;

    @Column(insertable = false, updatable = false)
    public Time futureEnd;

    //@Transient
    @Column(name = "locStart")
    @Convert(converter = MyConverter.class)
    public String locStartString;

    //@Transient
    @Column(name = "locEnd")
    @Convert(converter = MyConverter.class)
    public String locEndString;

    @Column(name = "todayStart")
    @Convert(converter = MyConverter.class)
    public String todayStartString;

    //@Transient
    @Column(name = "futureStart")
    @Convert(converter = MyConverter.class)
    public String futureStartString;

    // @Transient
    @Column(name = "futureEnd")
    @Convert(converter = MyConverter.class)
    public String futureEndString;

    @Column
    public Long modifiedBy;

    @Column
    public Timestamp modified;
}

我认为你的意思是你有时间格式 -

14:21 --> 02:21 PM

您想在数据库中维护该格式吗?

java.sql.Time 对象会将时间存储为 24 小时格式 HH24:MM,实际上是自纪元以来的秒数作为其对 java.util.Date 的包装。这意味着添加 AM/PM 是通过 SimpleDateFormat -

的表示问题
DateFormat dateFormat = new SimpleDateFormat("hh:mm a");

您可以将此作为 -

  • 使用 SDF 直接转换
  • NamedQuery - to_char(,'HH:MM')
  • 属性转换器

考虑 -

@Column
public Time locEnd;

如果我们将其转换为 HH:MM a -

格式的字符串
@Column
 @Convert(converter = DateStringConverter.class)
public String locEndString;

现在创建转换器 class -

import javax.persistence.AttributeConverter;
import javax.persistence.Converter;

@Converter
public class DateTimeConverter implements AttributeConverter {

 private static final dateFormat = new SimpleDateFormat("hh:mm a");

 @Override
 public Time convertToDatabaseColumn(String datehhmma) {
  return dateFormat.parse(datehhmma);
 }

 @Override
 public String convertToEntityAttribute(String datehhmma) {
  return dateFormat.parse(datehhmma);
 }

}

我得到了解决方案。它将通过这个过程来实现。

@Farrellmr 感谢您的支持

package util;

import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
import java.sql.Time;

/**
 * Created by aratan on 2/25/2015.
 */
@Converter
public class MyConverter implements AttributeConverter<String, Time> {
    @Override
    public Time convertToDatabaseColumn(String s) {
        System.out.println("::::=========================convertToEntityAttribute ::" + s);
        return new Time(System.currentTimeMillis());
    }

    @Override
    public String convertToEntityAttribute(Time time) {
        System.out.println("::::=========== convertToDatabaseColumn  ====== DateUtil.getFormattedTime(time) ::" + DateUtil.getFormattedTime(time));
        return DateUtil.getFormattedTime(time);
    }
}