简体   繁体   English

如何将Date = new Date转换为时间,然后将时间转换为long值

[英]How to convertDate = new Date to just time and then convert the time to a long value

i 一世

Does anyone know how I can convert my date which is currently "Tue Jun 14 10:21:46 SAST 2016" to time 10:21:46 and also to convert that time a long thereafter. 有谁知道如何将我的日期转换为当前“2016年6月14日星期二10:21:46”到时间10:21:46并且还要将此时间转换为该时间。 This is what I have so far and it doesnt seem to work quite well it leaves the "" which means I cant subtract current time from previous time 这是我到目前为止,它似乎没有工作得很好它留下了“”这意味着我不能减去前一次的当前时间

 Date d=new Date();
 SimpleDateFormat sdf=new SimpleDateFormat("hh:mm ");
 String timeNow = sdf.format(d);
 Long currentTime = Long.valueOf(timeNow).longValue();

This gives me accurate results 这给了我准确的结果

import java.util.Date;

public class myClass

{
 private Date date1 = null;

 public void start()
    {
       Date now = new Date();
      //assuming you did first do whatever you need to do with date1 prior to this 
      long timeDiffernce = (now.getTime() - date1.getTime()) / 1000;
     // this is milliseconds so divide by 1000 to get seconds
    }

}

You can use Calender class and then fetch the times from calender object. 您可以使用Calender类,然后从日历对象中获取时间。

public class CalendarTest {
  public static void main (String[] args) throws java.lang.Exception
  {
     Calendar cal = Calendar.getInstance();   // GregorianCalendar
     System.out.println("Calendar's toString() is : " + cal + "\n");
     System.out.println("Time zone is: " + cal.getTimeZone() + "\n");


    Date date = cal.getTime();
    System.out.println("Current date and time in Date's toString() is : " + date + "\n");
    System.out.println("Hour  : " + cal.get(Calendar.HOUR));
    System.out.println("Minute : " + cal.get(Calendar.MINUTE));
  }
}

You can use Calendar : 您可以使用日历:

Calendar cal = Calendar.getInstance();

SimpleDateFormat sdf=new SimpleDateFormat("hh:mm:ss");

String timeNow = sdf.format(cal.getTime());
Log.i("Time in hh:mm:ss ", timeNow);

Long currentTime = cal.getTimeInMillis();
Log.i("Time in milliseconds ", currentTime+"");

From comment : 来自评论

for example I want to then subtract 10:21:46 from 10:11:55 例如,我想从10:11:55减去10:21:46

Don't know if you want to just compare times, or if the date matters, so here I'll show both: 不知道你是否只想比较时间,或者日期是否重要,所以在这里我将展示:

TimeZone timeZone = TimeZone.getTimeZone("Africa/Johannesburg"); // or "Africa/Maseru" or "Africa/Mbabane"

String date1str = "Tue Apr 27 10:11:55 SAST 2016";
String date2str = "Tue Jun 14 10:21:46 SAST 2016";

SimpleDateFormat fmt1 = new SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy");
fmt1.setTimeZone(timeZone);
Date date1 = fmt1.parse(date1str);
Date date2 = fmt1.parse(date2str);

System.out.println("Time difference in seconds: " + (date2.getTime() - date1.getTime()) / 1000);

Calendar cal = Calendar.getInstance(timeZone);
cal.setTime(date1);
int secondOfDay1 = cal.get(Calendar.HOUR_OF_DAY) * 60 * 60 +
                   cal.get(Calendar.MINUTE) * 60 +
                   cal.get(Calendar.SECOND);
cal.setTime(date2);
int secondOfDay2 = cal.get(Calendar.HOUR_OF_DAY) * 60 * 60 +
                   cal.get(Calendar.MINUTE) * 60 +
                   cal.get(Calendar.SECOND);

System.out.println("Time difference in seconds: " + (secondOfDay2 - secondOfDay1));

Output 产量

Time difference in seconds: 4147791
Time difference in seconds: 591

The first line shows the total number of seconds between April 27 at 10:11:55 and June 14 at 10:21:46. 第一行显示4月27日10:11:55和6月14日10:21:46之间的总秒数。 4147791 seconds is 48d 9m 51s. 4147791秒是48d 9m 51s。

The first line shows the number of seconds between 10:11:55 and 10:21:46, ignoring the dates in question. 第一行显示10:11:55到10:21:46之间的秒数,忽略了有问题的日期。 591 seconds is 9m 51s. 591秒是9米51秒。

import java.util.Date;

public class myClass

{
 private Date date1 = null;

 public void start()
    {
       Date now = new Date();
      //assuming you did first do whatever you need to do with date1 prior to this 
      long timeDiffernce = (now.getTime() - date1.getTime()) / 1000;
     // this is milliseconds so divide by 1000 to get seconds
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM