Given a date D, I am trying to create two Calendar instances for that day at the following UTC times: 00:00:00 and 23:59:59. My algorithm should work regardless of my timezone (mine is UTC+1) and, given the day of D, for that D the time has to be set.
This is my code:
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
private static void f(Date date) {
Calendar c1 = Calendar.getInstance();
c1.setTime(date);
c1.set(Calendar.HOUR_OF_DAY, 0);
c1.set(Calendar.MINUTE, 0);
c1.set(Calendar.SECOND, 0);
Calendar c2 = Calendar.getInstance();
c2.setTime(date);
c2.set(Calendar.HOUR_OF_DAY, 23);
c2.set(Calendar.MINUTE, 59);
c2.set(Calendar.SECOND, 59);
//...
Then, I am trying to transform them into a UTC string with this code:
//...
String sc1 = fromDateToUTCString(c1.getTime());
String sc2 = fromDateToUTCString(c2.getTime());
System.out.println(sc1);
System.out.println(sc2);
}
public static String fromDateToUTCString(final Date date) {
final String ISO_FORMAT = "yyy-MM-dd'T'HH:mm:ss'Z'";
final SimpleDateFormat sdf = new SimpleDateFormat(ISO_FORMAT);
final TimeZone utc = TimeZone.getTimeZone("UTC");
sdf.setTimeZone(utc);
return sdf.format(date);
}
After this, the console prints 11:00:13 for c1 and 22:59:13 for c2.
JDK7.
The test main is trivial:
public static void main(String[] args) {
f(new Date());
}
Why?
I'm not sure what is your intention, but below code gives what you expect as the output.
...
Calendar c1 = Calendar.getInstance();
c1.setTimeInMillis(date.getTime());
c1.add(Calendar.HOUR_OF_DAY, -1*c1.get(Calendar.HOUR_OF_DAY));
c1.add(Calendar.MINUTE, -1*c1.get(Calendar.MINUTE));
c1.add(Calendar.SECOND, -1*c1.get(Calendar.SECOND));
Calendar c2 = Calendar.getInstance();
c2.setTimeInMillis(date.getTime());
c2.add(Calendar.HOUR, -1*c2.get(Calendar.HOUR_OF_DAY) + 23);
c2.add(Calendar.MINUTE, -1*c2.get(Calendar.MINUTE) + 59);
c2.add(Calendar.SECOND, -1*c2.get(Calendar.SECOND) + 59);
....
//comment the time zone setting
//sdf.setTimeZone(utc);
Output:
2015-01-21T 00:00:00Z
2015-01-21T 23:59:59Z
There is a Z at the end because of the format you have used. But actually the output is not in UTC.
Update:
With your code:
2015-01-20T16:30:00Z
2015-01-21T16:29:59Z
With your code + -Duser.timezone=GMT adding to JVM:
2015-01-21T00:00:00Z
2015-01-21T23:59:59Z
I assume you are not in UTC, so you're creating a time for timezone X and convert it to UTC afterwards. The solution is to create the Calendar objects in UTC:
c1.setTimeZone(TimeZone.getTimeZone("UTC"));
respectively
c2.setTimeZone(TimeZone.getTimeZone("UTC"));
Complete Sample Program
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
public class CalendarTest {
public static void main(String[] args) {
f(new Date());
}
private static void f(Date date) {
Calendar c1 = Calendar.getInstance();
c1.setTime(date);
c1.set(Calendar.HOUR_OF_DAY, 0);
c1.set(Calendar.MINUTE, 0);
c1.set(Calendar.SECOND, 0);
c1.setTimeZone(TimeZone.getTimeZone("UTC"));
Calendar c2 = Calendar.getInstance();
c2.setTime(date);
c2.set(Calendar.HOUR_OF_DAY, 23);
c2.set(Calendar.MINUTE, 59);
c2.set(Calendar.SECOND, 59);
c2.setTimeZone(TimeZone.getTimeZone("UTC"));
String sc1 = fromDateToUTCString(c1.getTime());
String sc2 = fromDateToUTCString(c2.getTime());
System.out.println(sc1);
System.out.println(sc2);
}
public static String fromDateToUTCString(final Date date) {
final String ISO_FORMAT = "yyy-MM-dd'T'HH:mm:ss'Z'";
final SimpleDateFormat sdf = new SimpleDateFormat(ISO_FORMAT);
final TimeZone utc = TimeZone.getTimeZone("UTC");
sdf.setTimeZone(utc);
return sdf.format(date);
}
}
leads to
2015-01-21T00:00:00Z
2015-01-21T23:59:59Z
Related
I need to convert date and time in hex code for writing it on IOT device.
Here is my code
private String getDateTimeToHexa() {
Calendar mCalendar = Calendar.getInstance();
TimeZone gmtTime = TimeZone.getTimeZone(TimeZone.getDefault().getDisplayName());
mCalendar.setTimeZone(gmtTime);
final Date date = mCalendar.getTime();
return Long.toHexString(date.getTime());
}
It is returning 11 digits hex code I need 8 digits, just like date and time in hex is 47C7EDE0 for this date 12:34:56 29/Feb/2008
Please help
import java.util.Calendar;
import java.util.Date;
public class Date {
public static void main(final String[] args)
{
final Calendar cal = Calendar.getInstance();
cal.set(Calendar.DATE, 15);
cal.set(Calendar.MONTH, Calendar.DECEMBER);
cal.set(Calendar.YEAR, 2005);
cal.set(Calendar.HOUR, 17);
cal.set(Calendar.MINUTE, 35);
cal.set(Calendar.SECOND, 20);
final Date date = cal.getTime();
System.out.printf("Date %s is encoded as: %s\n", date, Long.toHexString(date.getTime()));
// decode with: new Date(Long.parseLong("1082f469308", 16))
}
}
Try this:
private String getDateTimeToHexa() {
Calendar mCalendar = Calendar.getInstance();
TimeZone gmtTime = TimeZone.getTimeZone(TimeZone.getDefault().getDisplayName());
mCalendar.setTimeZone(gmtTime);
final Date date = mCalendar.getTime();
return Long.toHexString(date.getTime()/1000);
}
Instead of returning
return Long.toHexString(date.getTime());
Return following
return Long.toHexString(date.getTime()/1000);
As correctly pointed out by #shmosel that date.getTime() return time in a millisecond and if you want 8 digit Hex format then it needs to be converted in the second format.
The return type of Date can be found here
I have an date object from which i need to getTime(). The issue is it always shows 00:00:00.
SimpleDateFormat localDateFormat = new SimpleDateFormat("HH:mm:ss");
long date = Utils.getDateObject(DateObject).getTime();
String time = localDateFormat.format(date);
Why is the time always '00:00:00'. Should i append Time to my Date Object
You should pass the actual Date object into format, not a long:
SimpleDateFormat localDateFormat = new SimpleDateFormat("HH:mm:ss");
String time = localDateFormat.format(Utils.getDateObject(DateObject));
Assuming that whatever Utils.getDateObject(DateObject) is actually returns a Date (which is implied by your question but not actually stated), that should work fine.
For example, this works perfectly:
import java.util.Date;
import java.text.SimpleDateFormat;
public class SDF {
public static final void main(String[] args) {
SimpleDateFormat localDateFormat = new SimpleDateFormat("HH:mm:ss");
String time = localDateFormat.format(new Date());
System.out.println(time);
}
}
Re your comment below:
Thanks TJ, but actually i am still getting 00:00:00 as time.
That means your Date object has zeroes for hours, minutes, and seconds, like so:
import java.util.Date;
import java.text.SimpleDateFormat;
public class SDF {
public static final void main(String[] args) {
SimpleDateFormat localDateFormat = new SimpleDateFormat("HH:mm:ss");
String time = localDateFormat.format(new Date(2013, 4, 17)); // <== Only changed line (and using a deprecated API)
System.out.println(time);
}
}
Apart from above solution , you can also use calendar class if you don't have specific requirement
Calendar cal1 =new GregorianCalendar() or Calendar.getInstance();
SimpleDateFormat date_format = new SimpleDateFormat("HH:mm:ss");
System.out.println(date_format.format(cal1.getTime()));
For example, you can use next code:
public static int getNotesIndexByTime(Date aDate){
int ret = 0;
SimpleDateFormat localDateFormat = new SimpleDateFormat("HH");
String sTime = localDateFormat.format(aDate);
int iTime = Integer.parseInt(sTime);
return iTime;// count of hours 0-23
}
I have a date object myDate(say).
After doing the following:
Calendar cal = Calendar.getInstance();
cal.set(Calendar.MONTH, 9);
cal.set(Calendar.DATE, 24);
cal.set(Calendar.YEAR, 2013);
cal.set(Calendar.HOUR,13);
cal.set(Calendar.MINUTE,45);
cal.set(Calendar.SECOND,52);
I want to set the myDate object to the above values and hence show the below output.
Mon Sep 09 13:45:52 PST 2013
Please let me know how to do it.
import java.util.*;
public class DateTest {
public static void main(String[] args) {
Date myDate;
Calendar cal = Calendar.getInstance();
cal.set(Calendar.MONTH, 8);
cal.set(Calendar.DATE, 24);
cal.set(Calendar.YEAR, 2013);
cal.set(Calendar.HOUR,13);
cal.set(Calendar.MINUTE,45);
cal.set(Calendar.SECOND,52);
myDate = cal.getTime();
System.out.println(myDate);
}
}
The months attribute starts from January = zero. Why oh why did Sun do such a counter-intuitive thing, I don't know.
import java.util.*;
public class DateTest {
public static void main(String[] args) {
Date myDate;
Calendar cal = Calendar.getInstance();
cal.set(Calendar.MONTH, 8);
cal.set(Calendar.DATE, 24);
cal.set(Calendar.YEAR, 2013);
cal.set(Calendar.HOUR,13);
cal.set(Calendar.MINUTE,45);
cal.set(Calendar.SECOND,52);
myDate = cal.getTime();
System.out.println(myDate);
}
}
One other consideration, the output has a timezone in it - if you want the timezone of the Java installation, no probs. If you want PST and the Java timezone default is somewhere else, then use the following constructor:
TimeZone timezone = TimeZone.getTimeZone("America/Los_Angeles");
Calendar cal = Calendar.getInstance(timezone);
You can write:
myDate.setTime(cal.getTimeInMillis());
Using SimpleDateFormat:
new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy").format(cal.getTime())
I am using this code to get previous date but i would like to get the date excluding Saturday and Sunday
the code that i use to get previous date :
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class Main {
public static String previousDateString(String dateString)
throws ParseException {
// Create a date formatter using your format string
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
// Parse the given date string into a Date object.
// Note: This can throw a ParseException.
Date myDate = dateFormat.parse(dateString);
// Use the Calendar class to subtract one day
Calendar calendar = Calendar.getInstance();
calendar.setTime(myDate);
calendar.add(Calendar.DAY_OF_YEAR, -1);
// Use the date formatter to produce a formatted date string
Date previousDate = calendar.getTime();
String result = dateFormat.format(previousDate);
return result;
}
public static void main(String[] args) {
String dateString = "2012-08-20";
try {
// This will print 2012-08-19
System.out.println(previousDateString(dateString));
} catch (ParseException e) {
System.out.println("Invalid date string");
e.printStackTrace();
}
}
}`
It works fine but need to get the previous date which is not Saturday or Sunday.
Regards
You should have to get DAY_OF_WEEK from the calendar object and if its next day is MONDAY then subtract three days or if SUNDAY then subtract two days from the date/calendar object.
calendar.setTime(myDate);
int dayOfWeek=calendar.get(Calendar.DAY_OF_WEEK);
if(dayOfWeek==Calendar.MONDAY)
calendar.add(Calendar.DAY_OF_YEAR, -3);
else
if(dayOfWeek==Calendar.SUNDAY)
calendar.add(Calendar.DAY_OF_YEAR, -2);
else
calendar.add(Calendar.DAY_OF_YEAR, -1);
I thought I'd be able to create a GregorianCalendar using the constructor that takes the year, month, and day, but I can't reliably get those fields from an instance of the java.sql.Date class. The methods that get those values from java.sql.Date are deprecated, and the following code shows why they can't be used:
import java.sql.Date;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class DateTester {
public static void main(String[] args) {
Date date = Date.valueOf("2011-12-25");
System.out.println("Year: " + date.getYear());
System.out.println("Month: " + date.getMonth());
System.out.println("Day: " + date.getDate());
System.out.println(date);
Calendar cal = new GregorianCalendar(date.getYear(), date.getMonth(), date.getDate());
System.out.println(cal.getTime());
}
}
Here's the output, showing that the month and year are not returned correctly from the deprecated getYear() and getMonth() methods of Date:
Year: 111
Month: 11
Day: 25
2011-12-25
Thu Dec 25 00:00:00 EST 111
Since I can't use the constructor that I tried above, and there's no GregorianCalendar constructor that just takes a Date, how can I convert a java.sql.Date object into a GregorianCalendar?
You have to do this in two steps. First create a GregorianCalendar using the default constructor, then set the date using the (confusingly named) setTime method.
import java.sql.Date;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class DateTester {
public static void main(String[] args) {
Date date = Date.valueOf("2011-12-25");
System.out.println(date);
Calendar cal = new GregorianCalendar();
cal.setTime(date);
System.out.println(cal.getTime());
}
}
Here's the output:
2011-12-25
Sun Dec 25 00:00:00 EST 2011
I'm going from memory, but have you tried
Calendar cal = GregorianCalendar.getInstance();
cal.setTime(rs.getDate());
Try this.
import java.sql.Date;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class DateTester {
public static void main(String[] args) {
Date date = Date.valueOf("2011-12-25");
System.out.println(date);
Calendar cal = new GregorianCalendar();
cal.setTime(date);
System.out.println(cal.getTime());
}
}
Use setTimeInMillis():
java.sql.Date date = new java.sql.Date(System.currentTimeMillis());
Calendar cal = new GregorianCalendar();
cal.setTimeInMillis(date.getTime());
I think this is the simplest way.