我正在开发一个使用Spring和Hibernate的网站(该网站是关于股票交易).
每天凌晨12点左右,我需要取消所有订单.目前我的解决方案是使用每小时运行的计划任务:
<task:scheduled ref="ordersController" method="timeoutCancelAllOrders" fixed-delay="60*60*1000" />
Run Code Online (Sandbox Code Playgroud)
然后在方法timeoutCancelAllOrders中,我得到当前时间并检查,如果它是在晚上11点到12点之间然后执行任务
我看到它的方式,任务计划在我启动服务器时启动(我在Eclipse中使用Tomcat),但是当我在在线托管上部署它时(我正在使用Openshift),我不知道什么时候是启动时间任务时间表.
1:如何更自动地做到这一点?有什么像myTask.startAt(12AM)吗?
2:我住在越南,但服务器(Openshift)位于美国,所以这里是我的检查方式:
Date currentTime = new Date();
DateFormat vnTime = new SimpleDateFormat("hh:mm:ss MM/dd/yyyy ");
vnTime.setTimeZone(TimeZone.getTimeZone("Asia/Ho_Chi_Minh"));
String vietnamCurrentTime = vnTime.format(currentTime);
String currentHourInVietnam = vietnamCurrentTime.substring(0, 2);
System.out.println(currentHourInVietnam);
if(currentHourInVietnam.equals("00")){
// DO MY TASK HERE
}
Run Code Online (Sandbox Code Playgroud)
这看起来很愚蠢.我该如何改进我的代码?
我将 .Net Core 2.2 与实体框架一起使用。我需要将一些数据插入 SQL Server。
在 C# 中我使用DateTime数据类型,在 SQL Server 中我使用datetime2(6)- 默认值为:(sysdatetime())。
例如我的对象看起来像:
public partial class Contact
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime CreatedTime { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
如果我在创建对象时将 CreatedTime 保留为空,并让 SQL Server 使用默认值 (sysdatetime()),我将在数据库中得到类似以下内容的信息: 2020-05-19 09:31:12.383593 。
据我了解,DateTime 数据类型本身已经具有微秒精度,但是,如果我为 CreatedTime 属性设置一个值,例如:
obj.CreatedTime = DateTime.Now.AddTicks(123);
Console.WriteLine(obj.CreatedTime.ToString("yyyy-MM-dd HH:mm:ss.ffffff"));
Run Code Online (Sandbox Code Playgroud)
我可以在控制台中看到该值,它将是2020-08-18 07:42:52.131102。但是,当通过 EntityFramwork 插入该 obj 时,数据库中的值将仅为毫秒:2020-08-18 07:42:52.131000。看起来 EntityFramework 不知何故只以毫秒精确度传递 …