GVKun编程网logo

如何创建今天午夜和明天午夜的 Java Date 对象?

19

对于想了解如何创建今天午夜和明天午夜的JavaDate对象?的读者,本文将是一篇不可错过的文章,并且为您提供关于c#–将DateTime更改为午夜、datetime–Dart的最后午夜怎么样?、Dja

对于想了解如何创建今天午夜和明天午夜的 Java Date 对象?的读者,本文将是一篇不可错过的文章,并且为您提供关于c# – 将DateTime更改为午夜、datetime – Dart的最后午夜怎么样?、Django每天午夜在后台更新静态csv文件、java – 如何创建此类的对象?的有价值信息。

本文目录一览:

如何创建今天午夜和明天午夜的 Java Date 对象?

如何创建今天午夜和明天午夜的 Java Date 对象?

在我的代码中,我需要找到我今天发生的所有事情。所以我需要比较从今天上午 00:00(今早午夜)到下午 12:00(今晚午夜)的日期。

我知道 …

Date today = new Date();

......现在得到我。和 …

Date beginning = new Date(0);

… 让我在 1970 年 1 月 1 日获得零时间。但是,今天获得零时间和明天获得零时间的简单方法是什么?


更新

我这样做了,但肯定有更简单的方法吗?

Calendar calStart = new GregorianCalendar();calStart.setTime(new Date());calStart.set(Calendar.HOUR_OF_DAY, 0);calStart.set(Calendar.MINUTE, 0);calStart.set(Calendar.SECOND, 0);calStart.set(Calendar.MILLISECOND, 0);Date midnightYesterday = calStart.getTime();Calendar calEnd = new GregorianCalendar();calEnd.setTime(new Date());calEnd.set(Calendar.DAY_OF_YEAR, calEnd.get(Calendar.DAY_OF_YEAR)+1);calEnd.set(Calendar.HOUR_OF_DAY, 0);calEnd.set(Calendar.MINUTE, 0);calEnd.set(Calendar.SECOND, 0);calEnd.set(Calendar.MILLISECOND, 0);Date midnightTonight = calEnd.getTime();

答案1

小编典典

java.util.日历

// today    Calendar date = new GregorianCalendar();// reset hour, minutes, seconds and millisdate.set(Calendar.HOUR_OF_DAY, 0);date.set(Calendar.MINUTE, 0);date.set(Calendar.SECOND, 0);date.set(Calendar.MILLISECOND, 0);// next daydate.add(Calendar.DAY_OF_MONTH, 1);

JDK 8 - java.time.LocalTime 和 java.time.LocalDate

LocalTime midnight = LocalTime.MIDNIGHT;LocalDate today = LocalDate.now(ZoneId.of("Europe/Berlin"));LocalDateTime todayMidnight = LocalDateTime.of(today, midnight);LocalDateTime tomorrowMidnight = todayMidnight.plusDays(1);

乔达时间

如果您使用的是 JDK < 8,我推荐Joda Time,因为 API 非常好:
~~~~

DateTime date = new DateTime().toDateMidnight().toDateTime();DateTime tomorrow = date.plusDays(1);

由于 Joda Time 的 2.3 版DateMidnight弃用 ,因此请使用以下命令:

DateTime today = new DateTime().withTimeAtStartOfDay();DateTime tomorrow = today.plusDays(1).withTimeAtStartOfDay();

如果您不想要 JVM 的当前默认时区,请传递时区。

DateTimeZone timeZone = DateTimeZone.forID("America/Montreal");DateTime today = new DateTime(timeZone).withTimeAtStartOfDay(); // Pass time zone to constructor.

c# – 将DateTime更改为午夜

c# – 将DateTime更改为午夜

我有以下EffectiveDay,我想将日期时间更改为午夜.

public DateTime EffectiveDate { get; set; }

enter image description here

在屏幕截图中,它显示02/28/2018 5:00:00 AM.

我想把它改成02/28/2018 12:00:00 AM(午夜)

我尝试了以下方法,它没有用

ClassRoom.WorkOrders.Select(w => w.EffectiveDate).ToList().ForEach(s => s = s.Date);

我也尝试了以下方法,它没有用.

ClassRoom.WorkOrders.Select(w => w.EffectiveDate).ToList().ForEach(s => s = GetDateZeroTime(s.Date));

public static DateTime GetDateZeroTime(DateTime date)
{
  return new DateTime(date.Year,date.Month,date.Day,0);
}

解决方法

在ToList之后使用赋值执行ForEach不会有帮助,因为您正在设置s,即每次迭代后丢弃的参数.

使用普通的foreach循环来完成赋值:

foreach (var w in ClassRoom.WorkOrders) {
    w.EffectiveDate = w.EffectiveDate.Date;
    // w.EffectiveDate = GetDateZeroTime(w.EffectiveDate); would also work
}

datetime – Dart的最后午夜怎么样?

datetime – Dart的最后午夜怎么样?

我想这在大多数语言中都是非常常见的任务,但是我不清楚如何在我的Flutter应用程序中完成此操作.如何在Dart中检索最后一个午夜的DateTime对象?或者可能,今天/明天/昨天的任何特定时间?

解决方法

这应该做同样的事情

final Now = DateTime.Now();
final lastMidnight = new DateTime(Now.year,Now.month,Now.day);

你可以用这种方式来获得今天的不同时间

final tomorrowNoon = new DateTime(Now.year,Now.day + 1,12);

final yesterdaySixThirty = new DateTime(Now.year,Now.day - 1,6,30);

根据您的需要,绝对值可以更安全,因为添加/减去持续时间会因闰年和夏令时而导致意外.

Django每天午夜在后台更新静态csv文件

Django每天午夜在后台更新静态csv文件

通过使用batfile在Windows Tasksheduler中运行任务解决了该问题,详细信息可以在以下链接中找到

https://datatofish.com/python-script-windows-scheduler/

java – 如何创建此类的对象?

java – 如何创建此类的对象?

在B类中,如何创建除了对象创建过程之外的类A的对象(即,不创建具有null的对象)?
class A
  {
    public int one;  
   A(A a)
    {
      a.one=1;
    }
  }
class B
  {
   public static void main(String...args)
     {
     //Now how to create an object of class A over here.
     }
  }

解决方法

你通常不能用Java做到这一点.

然而,这可能是重作弊.不要在生产代码中执行此操作.但为了争论:

1)使用Mockito(但不能与安全管理员一起工作):

import org.mockito.Mockito;

class B {

    public static void main(String... args) {
        A mockedA = Mockito.mock(A.class);
        A realA = new A(mockedA);
        System.out.println(realA);
    }
}

2)这是another way to do it overriding finalize().

关于如何创建今天午夜和明天午夜的 Java Date 对象?的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于c# – 将DateTime更改为午夜、datetime – Dart的最后午夜怎么样?、Django每天午夜在后台更新静态csv文件、java – 如何创建此类的对象?等相关内容,可以在本站寻找。

本文标签: