如何在Java中使用剥离时间函数 [英] how to use strip time function in java

查看:95
本文介绍了如何在Java中使用剥离时间函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用"currentdate"检索了当前日期.这给了我当前的日期和时间,格式为yy/mm/dd hr:min:sec ..使用剥离时间函数,我想将时间设为00:00:00 ..但是有些方法对我不起作用.它为我提供了当前时间,而不是00:00:00 ..以下是我已完成的代码...

Hi,I have retrieved the current date using "currentdate"..this gives me current date and time in the following format yy/mm/dd hr:min:sec..using strip time function I wanna make the time as 00:00:00..But some how this is not working for me.it gives me the current time instead of 00:00:00..below is the code which I have done...

Date currentDate = new Date();
               stripTime(currentDate);


我也创建了striptime方法.


I have created the striptime method also.

public  static  Date stripTime(Date currentDate)
{

        if (currentDate == null)
            return null;

        Calendar cal = Calendar.getInstance();
        cal.setTime(currentDate);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);

        return cal.getTime();
}


让我知道需要进行哪些修改


Let me know what modifications are required

推荐答案

这会以以下格式yy/mm/dd hr:min:sec给出当前日期和时间

这是不正确的,对Date()的调用将返回一个代表自1970年1月1日以来的特定时间的对象.这取决于您如何显示该值.如果您只是想显示没有时间的日期,则需要相应地调整代码.
this gives me current date and time in the following format yy/mm/dd hr:min:sec

This is not correct, the call to Date() returns an object that represents a specific time since 1 January 1970. It is up to you how you display that value. If you are just trying to display the date without the time then you need to adjust your code accordingly.


SimpleDateFormat [ ^ ]是您的朋友.

因此,如果您只想要时间:
SimpleDateFormat[^] is your friend.

So if you want only the time:
SimpleDateFormat oFormat = new SimpleDateFormat("HH:mm:ss");
String strDate = oFormat.format(new Date());


这篇关于如何在Java中使用剥离时间函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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