I have various Date instances in my Java program. Working with the is a pain but it is required.
Date today = new Date(); // Wed Dec 10 14:10:29 EST 2014
Date a = new GregorianCalendar(2014, 11, 10).getTime();
Date b = new GregorianCalendar(2015, 01, 10).getTime();
Date c = new GregorianCalendar(2015, 02, 10).getTime();
Date d = new GregorianCalendar(2015, 03, 10).getTime(); //Fri April 10 00:00:00 EDT 2015
Date e = new GregorianCalendar(2015, 11 ,10).getTime();
I need to figure out how to shave off the time (14:10:29) from each as well as convert them to GMT time.
I know today.getTime(); will the number of milliseconds since January 1, 1970, 00:00:00 GMT, but I'm not sure how to represent that with out the times.
This would be for easier comparisons between dates. Thanks.
try using date formater
SimpleDateFormat df= new SimpleDateFormat("HH:MM:ss");
df.format(date)
All you have to do is modulus the long value by the long value of 1 day, and subtract that off the long value of your date.
If you have Wed Dec 10 14:10:29 EST 2014, then if you do
Date today = new Date(); // Wed Dec 10 14:10:29 EST 2014
long timeDiff = today.getTime() % 24 * 60 * 60 * 1000;
today = new Date(today.getTime - timeDiff);
The new today object will be created with the time of the day removed.
To convert them to GMT you can similarly create new dates for the longs. This is obviously "Date" way to do it. The best way would be to use Calendar.
Related
When I call the method date.toString() twice, I have the same result, even though there is a two second interval. I think the second time should be 2s more than first time.
Program for example:
Date date = new Date();
System.out.println(date.toString());
System.out.println("----------");
Thread.sleep(2000);
System.out.println(date.toString());
The output is
Sat Oct 17 17:54:39 CST 2015
----------
Sat Oct 17 17:54:39 CST 2015
Quoting the Javadoc of the constructor of Date (emphasis mine):
Allocates a Date object and initializes it so that it represents the time at which it was allocated, measured to the nearest millisecond.
As such, the Date that is printed is the date when the object was created. Since you are printing the same Date object, it will be the same output.
Date date = new Date();
System.out.println(date.toString());
System.out.println("----------");
Thread.sleep(2000);
date = new Date(); // instantiate a new Date here
System.out.println(date.toString());
Output on my machine:
Sat Oct 17 12:09:16 CEST 2015
----------
Sat Oct 17 12:09:18 CEST 2015
try this...
Date date = new Date();
System.out.println(date.toString());
System.out.println("----------");
Thread.sleep(2000);
date = new Date();
System.out.println(date.toString());
just initiate again the "date" before print for the second time
I would like to convert a Unix time stamp to readable DateTime.
when the value is 1423525935 i'm getting "January 17, 1970 01:25" and not "Feb 09 2015 23:52:15"
Thnx
my script
SimpleDateFormat sdf = new SimpleDateFormat("MMMM d, yyyy hh:mm ");
String date1 = sdf.format(properties.get("time"));
System.out.println(date1);
System.out.println(properties.get("time"));
and my output is
1423525935
January 17, 1970 01:25
1423525976
January 17, 1970 01:25
1423526012
January 17, 1970 01:25
1423526026
January 17, 1970 01:25
1423526047
January 17, 1970 01:25
1423526172
January 17, 1970 01:25
You appear to be using a seconds-based Unix Epoch value, whereas you require a time in milliseconds for your SimpleDateFormat. Thus, you must multiply your current Long values by 1000 in order to get the correct dates. See if that helps.
SimpleDateFormat sdf = new SimpleDateFormat("MMMM d, yyyy hh:mm ");
int time = Integer.parseInt(properties.get("time")); //if time property is not an int
Long l = time * 1000L;
String date1 = sdf.format(l);
System.out.println(date1);
This gives output of February 9, 2015 05:52.
As Elizion's answer mentioned you are using seconds based Epoch time, I converted into Milli seconds by multiplying 1000 to it.
I'm trying to create a function that convert a timestamp to Date object.
My problem is that using this online tools i reach correctly to convert timestamp to date but using java it doesn't convert correctly.
This is what i try:
public static Date getDateFromUUID(UUID uuid) {
Calendar uuidEpoch = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
uuidEpoch.clear();
uuidEpoch.set(1582, 9, 15, 0, 0, 0);
long epochMillis = uuidEpoch.getTime().getTime();
long time = (uuid.timestamp() / 10000L) + epochMillis;
Calendar start = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
Calendar end = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
start.setTimeInMillis(time*1000);
end.set(start.get(Calendar.YEAR), start.get(Calendar.MONTH), start.get(Calendar.DAY_OF_MONTH),0,0,0);
return end.getTime();
}
I'm trying using that uuid: a261ae00-2a9c-11b2-ae56-bcee7be23398
it correctly converts to timestamp : 1406412000
Using this:
Calendar start = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
Calendar end = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
start.setTimeInMillis(time*1000);
end.set(start.get(Calendar.YEAR), start.get(Calendar.MONTH), start.get(Calendar.DAY_OF_MONTH),0,0,0);
return end.getTime();
I need to remove hours, minutes and seconds and take only years,months and days.
but it convert timestamp to
Sat Jul 26 02:00:00 CEST 2014
Instead of
Sun Jul 27 00:00:00 CEST 2014
what could be my mistake?
Thanks!
Your time zone if wrong. Notice that output is CEST but you set the calendar to UTC. The delta between these two is 2 hours. When you output the Date you need to set the timezone appropriately.
I have three different times:
time on server - "Wed, 19 Feb 2014 11:44 CET"
time of start meeting on server - "12:00h"
time on device- "13:49"
I need to get time of start meeting on device ...this-> "14:00h" or time to meeting this-> "11m"
I'm trying to get it by using :
long ts = System.currentTimeMillis();
Date localTime = new Date(ts);
String gmt_time="12:00h";
SimpleDateFormat format = new SimpleDateFormat("HH:mm'h'");
format.setTimeZone(TimeZone.getTimeZone("GMT"));
Date d_date = null;
d_date = format.parse(gmt_time);
format.setTimeZone(TimeZone.getTimeZone("UTC"));
Date fromGmt = new Date(d_date.getTime() + TimeZone.getDefault().getOffset(localTime.getTime()));
String new_date=format.format(fromGmt);
But result in new_date is "15:00h" (I need "14:00h")
You assumption of CET = GMT on Wed, 19 Feb 2014 seems to be incorrect - refer to here.
CET is an hour ahead of GMT
11:44 CET would mean 10:44 GMT
Hence, when you calculate an offset from GMT time and your local time, it adds an hour to it.
Change format.setTimeZone(TimeZone.getTimeZone("GMT")); to format.setTimeZone(TimeZone.getTimeZone("CET")); and it should work as expected.
Found quickly solution
String cet_time="12.11.14"+" "+"12:00h";
SimpleDateFormat format = new SimpleDateFormat("dd.MM.yy' 'HH:mm'h'");
format.setTimeZone(TimeZone.getTimeZone("CET"));
Date d_date = format.parse(cet_time);
( new SimpleDateFormat("dd.MM.yy")).format(fromGmt);//new date String
( new SimpleDateFormat("HH:mm'h'")).format(fromGmt);//new time String
If we have 2 dates
Previous Date : Wed Jun 02 17:30:00 CDT 2010
Next Date : Sun Feb 13 22:00:00 CST 2011
and need to find difference in mins. between these 2 dates
Is there a way to accurately get it?
Yes, you can get an accurate difference of those times:
Parse each one with SimpleDateFormat to get a Date.
Get the time in milliseconds since the Epoch from each.
Subtract the two times and divide by 60000 for minutes.
Here's the code:
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
Date prevDate = sdf.parse("Wed Jun 02 17:30:00 CDT 2010");
Date nextDate = sdf.parse("Sun Feb 13 22:00:00 CST 2011");
long diffTime = nextDate.getTime() - prevDate.getTime();
System.out.println(diffTime / 60000 + " minutes");
date1.getTime() - date2.getTime() will give you the difference in milliseconds. You can then divide it by 60000 to get the difference in minutes.
Use TimeUnit class for convertion.
// specify the input format
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
String s1 = "Wed Jun 02 17:30:00 CDT 2010";
String s2 = "Sun Feb 13 22:00:00 CST 2011";
// parse to Date object
Date d1 = dateFormat.parse(s1);
Date d2 = dateFormat.parse(s2);
// get time in milliseconds
long l1 = d1.getTime();
long l2 = d2.getTime();
// absolute difference
long diff = Math.abs(l1 - l2);
// convert milliseconds to minute
long min = TimeUnit.MILLISECONDS.toMinutes(diff);
System.out.println(min);
With Joda Time
DurationFormatUtils.formatDuration(date2.getTime() - date1.getTime(), "m");