Why date.toString() twice to print the same result - java

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

Related

Negative Timestamp Java

I have wierdo problem with timestamp in Java/Android
Date inputDate = null;
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm dd/MM/yyyy");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
Log.e("DateH:D", hour+" "+day);
try {
inputDate = sdf.parse(hour + " " + day);
Date currentDate = new Date();
Log.e("InputDate", inputDate.toString());
Log.e("InputDate",inputDate.getTime()+"");
Log.e("CurrentDate", currentDate.toString());
Log.e("CurrentDate",currentDate.getTime()+"");
if (!inputDate.after(currentDate) ){
//TODO change this string
hourField.setError("Date from past");
return false;
}
} catch (ParseException e) {
Log.e("DateParser" , e.getLocalizedMessage);
return false;
}
Example output for this is:
DateH:D: 12:33 15/09/16
E/InputDate: Tue Sep 15 14:33:00 CET 16
E/InputDate: -61640134020000
E/InputDate: Tue Sep 15 14:33:00 CET 16
E/CurrentDate: Thu Sep 15 11:38:43 CEST 2016
E/CurrentDate: 1473932323198
So non-timestamp representation of date is correct but timestamp is wrong. How it's possible? What i'm doing wrong?
Use yy to parse 2-digit years.
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm dd/MM/yy");
The getTime method of Date:
Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT
represented by this Date object.
Since the year you specified is year 16, the negative result makes sense.
The starting date for epoch milis is Thu, 01 Jan 1970 00:00:00 GMT. Any datetime before that is negative in epoch milis.

DateUtils.addDays() class issue in daylight saving time

Recently New Zealand observed daylight saving on 27 sept 15.
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
sd.setTimeZone(TimeZone.getTimeZone("Pacific/Auckland"));
Date dateValue = sd.parse("2015-09-30");
System.out.println(dateValue); // prints "Tue Sep 29 07:00:00 EDT 2015" My local system timzone in EDT
dateValue = DateUtils.addDays(dateValue, -6); // 6 days back 24 Sep of Pacific/Auckland
System.out.println(dateValue); // prints "Tue Sep 23 07:00:00 EDT 2015"
The second print statement should print Tue Sep 29 08:00:00 EDT 2015, as Daylight Saving not is in effect.
The issue is before 27 Sep 15 NZ = UTC+12
and after NZ = UTC +13
So on date of 23 Sep It should have time 08:00:00 not 07:00:00
The problem is within DateUtils.addDays from Apache Commons: it is using a Calendar with the default timezone to add and subtract days instead of using a user-supplied timezone. You can see this in the source code of the method add: it calls Calendar.getInstance() and not Calendar.getInstance(someTimezone)
If you construct yourself the Calendar and set the correct timezone, the problem disappears:
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
sd.setTimeZone(TimeZone.getTimeZone("Pacific/Auckland"));
Date dateValue = sd.parse("2015-09-30");
System.out.println(dateValue); // prints "Tue Sep 29 13:00:00 CEST 2015"
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("Pacific/Auckland")); // set correct timezone to calendar
calendar.setTime(dateValue);
calendar.add(Calendar.DAY_OF_MONTH, -6);
dateValue = calendar.getTime();
System.out.println(dateValue); // prints "Wed Sep 23 14:00:00 CEST 2015"
also i have used joda api to resolved this timezone issue.
org.joda.time.DateTimeZone timeZone = org.joda.time.DateTimeZone.forID( "Pacific/Auckland" );
DateTime currentDate= new DateTime( new Date(), timeZone );
DateTime dateValue = now.plusDays( -6 ); // prints Tue Sep 29 08:00:00 EDT 2015

Using Date in Java

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.

How to convert CET time to local time on device?

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

Invalid date value after parse

I am new to java. I have a Date that is stored in the variable, pubDate = "2013-09-23"
When I'm executing this
SimpleDateFormat pubSimpleDateFormat = new SimpleDateFormat("yyyy-mm-dd");
Date publishDate = pubSimpleDateFormat.parse(pubDate);
I'm getting wrong value : Wed Jan 23 00:09:00 GMT+05:30 2013
Please help me why it so. and help me to solve this.
M is for Month in year while m is for Minute in hour
You should use SimpleDateFormat pubSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
String pubDate = "2013-09-23";
SimpleDateFormat pubSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date publishDate = pubSimpleDateFormat.parse(pubDate);
System.out.println(publishDate);
Output :
Mon Sep 23 00:00:00 GMT 2013
Read the section Date and Time Patterns.

Categories