`Timestamp`和`Time`自动转换

时间:2016-12-09 10:05:20

标签: java android mysql hibernate timestamp

我正在使用我的移动应用程序连接到我自己开发的REST API(使用JerseyHibernate构建)。我在那里保存了记录创建时间和上次更新时间,即数据库中的botg timestamps,即使它们在我的Hibernate实现中被标识为java.util.Date。以下是我从手机上保存的方式。

Patient patient = new Patient();
patient.setIdpatient(idPatient);

UnitType unitType = new UnitType();
unitType.setIdunitType(idUnitType);

Units units = new Units();
units.setPatient(patient);
units.setUnitType(unitType);
units.setUnitMeasurement(hba1c);
units.setDateCreated(Common.getSQLCurrentTimeStamp());
units.setLastUpdated(Common.getSQLCurrentTimeStamp());

restCallSaveUnits(units);


private void restCallSaveUnits(Units units) {
    Gson gson = new GsonBuilder()
            .setDateFormat("yyyy-MM-dd'T'HH:mm:ss")
            .create();

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(RestCommon.URL)
            .addConverterFactory(GsonConverterFactory.create(gson))
            .build();

    UnitsEndPoint endPoint = retrofit.create(UnitsEndPoint.class);
    Call<ResponseBody> call = endPoint.saveUnit(units);
    call.enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Response<ResponseBody> response, Retrofit retrofit) {
            try {
                String saveMessage = response.body().string();

                Log.d("UNIT_ACTIVITY", " SAVE_MESSAGE " + saveMessage);
                Toast.makeText(context, saveMessage, Toast.LENGTH_SHORT).show();

                isWeightLoaded = true;
                isHeightLoaded = true;
                isGluLoaded = true;
                isHbLoaded = true;

            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onFailure(Throwable t) {
            t.printStackTrace();
        }
    });
}

设置时间时,我会得到当前的时间戳。这是由Common.getSQLCurrentTimeStamp())完成的,我已经提到过以下方法。

public static java.sql.Timestamp getSQLCurrentTimeStamp()
{
    java.util.Date date= new java.util.Date();
    Timestamp t= new Timestamp(date.getTime());
    System.out.println(t);
    return t;
}

我的时区是GMT +5.30。服务器为Amazon EC2,数据库为Amazon RDS - MySql。无论如何,时间总是在+ 5.30H额外保存。这意味着,如果发送到服务器的时间为13:30,则保存的内容为18:30。当它返回数据时,我得到Timestamps作为一个长值,时间就像服务器保存的那样,18:30。同样适用于Time字段。

我已经尝试了很多方法来解决这个问题,非常有效。服务器收到它的时候似乎已经改变了,因为我在实际保存到数据库之前打印了它。

我的应用程序将供来自不同国家/地区的用户使用(因为它将在PlayStore上),我无法弄清楚如何使用timestamptime解决此问题。有什么想法吗?

0 个答案:

没有答案