如何从java中的特定时间戳获取当天的开始日期

时间:2016-06-16 07:01:18

标签: java time

样品:

Long timeStamp = 1466058808;
Time time = new Time(timeStamp );
DateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
System.out.println(format.format(time));

打印时间:2016-05-31 08:19:07

我如何得到预期的结果是:2016-05-31 00:00:00

谢谢!

最后得到2016-05-31 00:00:00的时间戳这就是我想要的

所以任何人都知道如何制作它?

3 个答案:

答案 0 :(得分:6)

简单,使用:

DateFormat format = new SimpleDateFormat("yyyy-MM-dd 00:00:00");

或者如果你想要一个长(并且有java 8):

LocalDateTime.ofEpochSecond(1466058808, 0, ZoneOffset.UTC).toLocalDate().atStartOfDay().toEpochSecond(ZoneOffset.UTC)

答案 1 :(得分:0)

使用日历,以便您可以提取日期属性:

@interface ViewController ()

@property (strong, nonatomic) UIView *currentPicture;

@end

@implementation ViewController

- (void)viewDidLoad {
     [super viewDidLoad];

     self.scaleSwitch.on = self.rotationControl.on = self.translationSwitch.on = NO;
     UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMidX(self.view.bounds)-50,
                                                          150,
                                                          100, 100)];

    myView.backgroundColor = [UIColor redColor];
    [self.view addSubview:myView];

    self.currentPicture = myView;    
}

#pragma mark - Slider Actions

- (IBAction)actionAnimationSpeedSlider:(id)sender {
    self.animationSpeedLabelInfo.text = [NSString stringWithFormat:@"%.3f", self.animationSpeedSlider.value];
}


#pragma mark - Switch Actions

- (IBAction)actionSwitch:(UISwitch *)sender {

    if (self.rotationControl.on)
        [self rotatePicture];
    else
        [sender.layer removeAllAnimations];  
 }


-(void) rotatePicture {
    CGAffineTransform currentPictureState = self.currentPicture.transform;

    [UIView animateWithDuration:self.animationSpeedSlider.value
                      delay:0.3f
                    options:UIViewAnimationOptionCurveLinear|UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionRepeat
                 animations:^{
                     self.currentPicture.transform = CGAffineTransformRotate(currentPictureState, M_PI);
                 }
                 completion:^(BOOL finished) {
                     NSLog(@"Rotation finished!Repeating.");
                 }];
}

答案 2 :(得分:0)

java.time

Answer by krzyk已接近但忽略了时区的关键问题。让我们再次尝试使用Java 8及更高版本中内置的java.time框架。大部分java.time功能都被反向移植到Java 6& 7(ThreeTen-Backport)并进一步适应Android(ThreeTenABP)。

纪元的秒数​​

你在问题​​中没有这么说,但我们假设Long的{​​{1}}是1970年第一时epoch UTC的整数秒}。

1466058808

首先我们将其转换为Instant,即时间轴上的一个时刻。

Instant

时区

确定日期和一天的开始取决于时区。对于任何给定的时刻,日期可以按时区在世界范围内变化。在巴黎午夜过后的一段时间,蒙特利尔仍然是“昨天”。

Instant instant = Instant.ofEpochSecond( 1466058808L );

您可以在代码中广泛使用ZonedDateTime。通常,最佳做法是以UTC格式执行业务逻辑,数据存储,日志记录等。仅在需要时应用时区,例如向用户显示。应用ZoneId获取ZonedDateTime

Instant

使用ZoneId zoneId = ZoneId.of( "America/Montreal" ); ZonedDateTime zdt = ZonedDateTime.ofInstant( instant , zoneId ); 作为开始日期

要获得当天的开始,我们需要通过LocalDate课程。请注意,我们应该始终传递可选的时区参数。如果省略,则隐式应用JVM的当前默认时区。该默认值可以随时更改,甚至在运行时期间,因为JVM的任何应用程序中的任何代码都可以调用TimeZone.setDefault。更好的具体。

LocalDate

请注意,假设当天从LocalDate localDate = zdt.toLocalDate(); ZonedDateTime startOfDay = localDate.atStartOfDay( zoneId ); 开始。 Daylight Saving Time (DST)可能意味着这一天从另一个wall-clock time开始。

一衬垫

我不建议这样做,但你可以将所有这些组合成一行。

00:00:00.0