My code:
Calendar c = Calendar.getInstance();
c.setTime(res.getDate("my_date")); //res is ResultSet
System.out.println(c.getTime());
System.out.println(res.getString("my_date"));
Output:
Thu Oct 11 00:00:00 IST 2012
2012-10-11 02:50:00.0
In calendar, it is not considering the time part. I used Calendar because I have to make some comparisons.
Why time part is omitted?
You should use something like :
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
out.println(sdf.format(rs.getTimestamp("your_date_field").getTime()));
By the way, you have to create a SimpleDateFormat that fit your SQL date format.
Related
I am converting a GregorianCalendar instance to a Date to get a unix timestamp.
But I was wondering why the same date returns different Unix timestamps each time.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Calendar calendar = new GregorianCalendar();
calendar.set(2018, 0, 1, 0,0,0);
System.out.println(sdf.format(calendar.getTime()));
Date date = calendar.getTime();
System.out.println(sdf.format(date));
System.out.println(date.getTime());
The date itself is correct and is always the same, "2018/01/01 00:00:00". But why is the unix timestamp different each time? For example, these are the values after 5 executions.
1514761200624
1514761200618
1514761200797
1514761200209
1514761200132
When you create a new calendar it contains current date and time. After that you update all fields EXCEPT milliseconds. As you can see only last 3 numbers differs in all your outputs, it is milliseconds of the execution time.
java.time
ZoneId zone = ZoneId.of("Europe/Brussels");
ZonedDateTime start2018 = LocalDate.of(2018, Month.JANUARY, 1).atStartOfDay(zone);
Instant asInstant = start2018.toInstant();
System.out.println(asInstant.toEpochMilli());
This consistently gives the following output:
1514761200000
Please substitute your desired time zone if it didn’t happen to be Europe/Brussels.
To format for output:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
System.out.println(start2018.format(formatter));
2018/01/01 00:00:00
The date and time classes that you were using — SimpleDateFormat, Calendar, GregorianCalendar and Date — are all poorly designed and long outdated. SimpleDateFormat in particular is notoriously troublesome, but in this case it was the poor design of Calendar that gave you unexpected results. The other answers have already explained how, there is no need for me to repeat. Instead of the old classes I recommend that you use java.time, the modern Java date and time API. It is so much nicer to work with.
Link: Oracle tutorial: Date Time explaining how to use java.time.
In timestamp, last 3-digit represents the milliseconds. Here you are explicitly setting the date and time but not the milliseconds. That's why you are facing this. To avoid this you can add this to your code:
calendar.set(Calendar.MILLISECOND, 0);
I am assuming you are instantiating everything within a loop in your example?
If so, you are not setting the milliseconds difference, so they change (however slightly) in each iteration of the loop.
To avoid this, you could either set the milliseconds, or do the instantiation outside of the loop:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Calendar calendar = new GregorianCalendar();
calendar.set(2018, 0, 1, 0, 0, 0);
for (int i = 0; i < 5; i++) {
System.out.println(sdf.format(calendar.getTime()));
Date date = calendar.getTime();
System.out.println(sdf.format(date));
System.out.println(date.getTime());
}
This will produce:
2018/01/01 00:00:00
2018/01/01 00:00:00
1514764800128
2018/01/01 00:00:00
2018/01/01 00:00:00
1514764800128
2018/01/01 00:00:00
2018/01/01 00:00:00
1514764800128
2018/01/01 00:00:00
2018/01/01 00:00:00
1514764800128
2018/01/01 00:00:00
2018/01/01 00:00:00
1514764800128
http://ideone.com/T5wSRV this is the link to below code
SimpleDateFormat dateFormatIST = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
dateFormatIST.setTimeZone(TimeZone.getTimeZone("IST"));
//Time in IST
Date date=dateFormatIST.parse( dateFormatIST.format(new Date()) );
System.out.println(date);
this is not giving correct IST time where as code below is working fine . why?
http://ideone.com/9KSaZx this is the link to below code which is giving the desired output.Help me understand the behavior.
SimpleDateFormat dateFormatIST = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
dateFormatIST.setTimeZone(TimeZone.getTimeZone("IST"));
//Local time zone
SimpleDateFormat dateFormatLocal = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
//Time in IST
Date date=dateFormatLocal.parse( dateFormatIST.format(new Date()) );
System.out.println(date);
The behaviour is logical. The point is that there is no information of time-zone is a Date object. A Date object contains Universal Time.
And when you format then parse the formatted string, you still have the same date:
I commented the code with the results:
SimpleDateFormat dateFormatIST = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
dateFormatIST.setTimeZone(TimeZone.getTimeZone("IST"));
//Local time zone
SimpleDateFormat dateFormatLocal = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
//Time in IST
Date d = new Date();
System.out.println(d);
// Mon Mar 16 16:57:19 CET 2015
=> now in my TZ (CET)
System.out.println(dateFormatIST.format(d));
// 2015-Mar-16 21:27:19
=> now in IST TZ
System.out.println(dateFormatLocal.format(d));
// 2015-Mar-16 16:57:19
=> now in my TZ (CET)
Date dateIST = dateFormatIST.parse(dateFormatIST.format(d));
System.out.println(dateIST);
// Mon Mar 16 16:57:19 CET 2015
=> The dateIST object contains still "now", and the format is default local which is CET
Date dateLoc = dateFormatLocal.parse(dateFormatLocal.format(d));
System.out.println(dateLoc);
// Mon Mar 16 16:57:19 CET 2015
=> same thing as above
Date dateLocIST = dateFormatLocal.parse(dateFormatIST.format(d));
System.out.println(dateLocIST);
// Mon Mar 16 21:27:19 CET 2015
=> dateFormatIST.format(d) gives "2015-Mar-16 21:27:19", and dateFormatLocal.parse() will interpret it like a local (CET for me) date. The result is then "Mon Mar 16 21:27:19 CET 2015".
If you need to translate dates between different time-zone, you certainly need to go for the Calendar class.
I ma trying to get the output time of "Europe/Stockholm" timezone whatever my computer local is (I have currently got GMT 0 on my laptop).
TimeZone tz = TimeZone.getTimeZone("Europe/Stockholm");
Calendar cal = Calendar.getInstance(tz);
Date date = new Date();
cal.setTime(date);
cal.setTimeZone(tz);
long curSeconds = (cal.getTimeInMillis()/(long)1000);
Log.v("Karl","new Date() "+cal.getTime());
But this still outputs:
new Date() Wed Mar 04 17:33:53 GMT+00:00 2015
I would like it to display the timezone of "Europe/Stockholm" as said!
When you call cal.getTime() you get java.util.Date. java.util.Date has no time zone - it is just a point in timeline, without location. (I mean you lost your calendar timezone when you cal.getTime())
java.util.Date.toString() displays date in host timezone (Timezone.getDefault())
That was a theory. Practice:
DateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
df.setTimeZone(tz);
System.out.println(df.format(cal.getTime()));
You will get
Wed Mar 04 22:01:53 CET 2015
This is both a duplicate and not a duplicate!
Just please help me and don't refer me to anywhere else, cause I'm really unable to get the GMT time.
The answer seems easy but it doesn't work for me.
I don't know are the answers all aver the web wrong, or am I making a mistake?
Please take a look at this snippet and the results:
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
long time = cal.getTimeInMillis();
Date date = new Date(time);
System.out.println(date);
time = System.currentTimeMillis();
date = new Date(time);
System.out.println("--\n" + date);
result :
Fri Feb 28 16:07:12 GMT+03:30 2014--
Fri Feb 28 16:07:12 GMT+03:30 2014
Both show my local time. I even printed directly the time, cause I thought maybe this is due to Date class but even those are the same (with just about 1 or 2 milliseconds difference).
Use setTimeZone() on a SimpleDateFormat to print the date in a specific timezone.
DateFormat format = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss z");
Date d = format.parse("28-Feb-2014 13:00:00 PST");
System.out.println(format.format(d));
format.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println(format.format(d));
Prints:
28-Feb-2014 13:00:00 PST
28-Feb-2014 21:00:00 GMT
Try the below
SimpleDateFormat f = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
f.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println(f.format(new Date(System.currentTimeMillis())));
One way of doing it is using SimpleDateFormat and the setTimeZone method():
SimpleDateFormat sdf = new SimpleDateFormat( "..." );
sdf.setTimeZone( TimeZone.getTimeZone( "GMT" ) ); // or UTC
System.out.println( sdf.format( date ) );
Cheers,
Is it possible to remove the day (Fri), the time (22:34:21) and the time zone (GMT) by just having an output like "Jan 11 1980" instead of "Fri Jan 11 22:34:21 GMT 1980"??
Code below:
Calendar date = Calendar.getInstance();
date.set(Calendar.YEAR, 1980);
date.set(Calendar.MONTH, 0);
date.set(Calendar.DAY_OF_MONTH, 11);
Date dob = date.getTime();
System.out.println(dob);//Fri Jan 11 22:34:21 GMT 1980
Many thanks!
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd yyyy");
System.out.println(sdf.format(date));
Output:
Feb 26 2013
If you want a specific date, do
Calendar c = Calendar.getInstance();
c.set(1980, 0, 11);
Date date = c.getTime();
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd yyyy");
System.out.println(sdf.format(date));
Prints
Jan 11 1980
public class DateFormat {
public static void main(String[] args) {
Calendar date = Calendar.getInstance();
date.set(Calendar.YEAR, 1980);
date.set(Calendar.MONTH, 0);
date.set(Calendar.DAY_OF_MONTH, 11);
Date dob = date.getTime();
System.out.println(new SimpleDateFormat("MMM dd yyyy").format(dob));
}
}
Output:
Jan 11 1980
Date is a representation of the number of milliseconds since the epoch (January 1, 1970, 00:00:00 GMT)
In order to "remove" the time portion of a Date, you will want to use a DateFormat
Something as simple as;
System.out.println(new SimpleDateFormat("dd/MM/yyyy").format(dob));
Should work.
For a more localised version, you should use DateFormat.getDateInstance()
System.out.println(DateFormat.getDateInstance().format(dob));
System.out.println(DateFormat.getDateInstance(DateFormat.SHORT).format(dob));
System.out.println(DateFormat.getDateInstance(DateFormat.MEDIUM).format(dob));
System.out.println(DateFormat.getDateInstance(DateFormat.LONG).format(dob));
DateFormat dateFormatter = DateFormat.getDateInstance();
System.out.println(dateFormatter.format(date);
This will print the only the date corresponding to your current system locale settings.
See also: DateFormat in the JavaDoc
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd yyyy");
System.out.println(sdf.format(dob));
you can use:
stringToPrint = time.getMonth()+" "+time.getDate()+" "+time.getYear();
for more info:
http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Date.html