什么是增加Java.sql.date / time的最佳方法? [英] What is the best way to increment a Java.sql.date / time?

查看:152
本文介绍了什么是增加Java.sql.date / time的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我有一个MySQL数据库存储日期和时间在单独的列。但是,在Java代码中,我需要以数分钟,数小时或数天的形式从数据库中增加日期和时间的结果时间戳,然后更新数据库中的相应列。



我目前在Java中使用Java.sql.date和java.sql.time类型。任何人都可以为此建议最好的方法吗?



我可以通过以下方式实现上述:

  public static String addToDateTime(String timestampIn,String increment){

//分解时间戳。
int year = Integer.parseInt(timestampIn.substring(0,4));
int month = Integer.parseInt(timestampIn.substring(5,7));
int day = Integer.parseInt(timestampIn.substring(8,10));
int hours = Integer.parseInt(timestampIn.substring(11,13));
int mins = Integer.parseInt(timestampIn.substring(14,16));
int secs = Integer.parseInt(timestampIn.substring(17,19));

日历calendar = new GregorianCalendar(year,month - 1,day,hours,mins,secs);

//递增时间戳。
if(increment.equals(HOURLY)){
calendar.add(Calendar.HOUR,1);
}
else if(increment.equals(DAILY)){
calendar.add(Calendar.HOUR,24);
}
else if(increment.equals(WEEKLY)){
calendar.add(Calendar.HOUR,168);
}
else if(increment.equals(DO NOT POLL)){

//什么也不做。

}

//撰写新的时间戳。
SimpleDateFormat sdf = new SimpleDateFormat(yyyy-MM-dd HH:mm:ss);
String timestampOut = sdf.format(calendar.getTime());
return timestampOut;
}

但是喜欢不太原始的东西。



谢谢



摩根先生

解决方案

你可以使用Joda时间。然后,您可以使用DateTime的 plusHours plusDays plusWeeks 。要解析,请 DateTimeFormat DateTimeFormatter 。如下:

  DateTimeFormatter fmt = DateTimeFormat.forPattern(YYYY-MM-dd HH:mm:ss); 
DateTime dt = fmt.parseDateTime(timestampin);

if(increment.equals(HOURLY)){
dt = dt.plusHours(1);
}
else if(increment.equals(DAILY)){
dt = dt.plusDays(1);
}
else if(increment.equals(WEEKLY)){
dt = dt.plusWeeks(1);
}

String timestampOut = fmt.print(dt);

您可以使用以下内容:

  DateTime dt = new DateMidnight(sqlDate).plus(Period.millis(sqlTime.getTime())); 

假设sql.Time表示自午夜以来的毫秒数。


I have an issue where I have a MySQL database storing dates and times in separate columns. However, in Java code I need to increment the resulting timestamp for a date and time from the database by minutes, hours or days and then update the respective columns in the database.

I'm currently using Java.sql.date and java.sql.time types in Java. Can anyone suggest the best method for this?

I can achieve the above by the following:

public static String addToDateTime(String timestampIn, String increment) {

    // Decompose timestamp.
    int year = Integer.parseInt(timestampIn.substring(0, 4));
    int month = Integer.parseInt(timestampIn.substring(5, 7));
    int day = Integer.parseInt(timestampIn.substring(8, 10));
    int hours = Integer.parseInt(timestampIn.substring(11, 13));
    int mins = Integer.parseInt(timestampIn.substring(14, 16));
    int secs = Integer.parseInt(timestampIn.substring(17, 19));

    Calendar calendar = new GregorianCalendar(year, month - 1, day, hours, mins, secs);

    // Increment timestamp.
    if (increment.equals("HOURLY")) {
        calendar.add(Calendar.HOUR, 1);
    }
    else if (increment.equals("DAILY")) {
        calendar.add(Calendar.HOUR, 24);
    }
    else  if (increment.equals("WEEKLY")) {
        calendar.add(Calendar.HOUR, 168);
    }
    else if (increment.equals("DO NOT POLL")) {

        // Do nothing.

    }

    // Compose new timestamp.
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String timestampOut = sdf.format(calendar.getTime());
    return timestampOut;
}

But would prefer something less primitive.

Thanks

Mr Morgan

解决方案

You can use Joda time. Then, you can use DateTime's plusHours, plusDays, and plusWeeks. For parsing, there is DateTimeFormat and DateTimeFormatter. Something like:

DateTimeFormatter fmt = DateTimeFormat.forPattern("YYYY-MM-dd HH:mm:ss");
DateTime dt = fmt.parseDateTime(timestampin);

if (increment.equals("HOURLY")) {
    dt = dt.plusHours(1);
}
else if (increment.equals("DAILY")) {
    dt = dt.plusDays(1);
}
else  if (increment.equals("WEEKLY")) {
    dt = dt.plusWeeks(1);
}

String timestampOut = fmt.print(dt);

You can probably use something like:

DateTime dt = new DateMidnight(sqlDate).plus(Period.millis(sqlTime.getTime()));

That assumes the sql.Time represents the number of milliseconds since midnight.

这篇关于什么是增加Java.sql.date / time的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆