Format a Date object with DateFormat in Java - java

I'm trying to learn about Date objects and the DateFormat class and I keep getting an error in the examples I'm trying to do. The goal is to get a due date by adding 30 days to a pretend invoice date, and then to format that due date. The dueDate method, I believe, is correct, but I'm having trouble formatting it properly.
Here is the first thing I have that takes the invoice date and adds 30 days to it.
public Date getDueDate()
{
Calendar cal = new GregorianCalendar();
cal.setTime(getInvoiceDate());
cal.add(Calendar.DATE, 30);
Date dueDate = cal.getTime();
return dueDate;
}
The next part is where I'm having the trouble, as it keeps telling me it expects a Date object but is receiving a String and I'm not sure why, as I'm supplying a Date object.
public Date getFormattedDueDate()
{
Date dueDate = getDueDate();
DateFormat shortDate = DateFormat.getDateInstance(DateFormat.SHORT);
return shortDate.format(dueDate);
}
Can anyone help me figure out why it's telling me that my supplied variable (dueDate) is a String when it's coded as a Date object?

format(Date date) Formats a Date into a date/time String.

Shamse is right
shortDate.format(dueDate);
returns a String, you can easly fix this changing your return type
public String getFormattedDueDate()
{
Date dueDate = getDueDate();
DateFormat shortDate = DateFormat.getDateInstance(DateFormat.SHORT);
return shortDate.format(dueDate);
}

The answer by Shamse is correct.
For the heck of it, here's the same kind of code but:
Written using the third-party library, Joda-Time 2.3
Care taken with time zones. Depending on default time zones is risky.
// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.
java.util.Date date = new Date(); // = getInvoiceDate();
org.joda.time.DateTime invoiceStoredDateTime = new org.joda.time.DateTime( date );
// Set to desired time zone. Ideally that invoice date was stored in UTC.
// Time Zone list: http://joda-time.sourceforge.net/timezones.html
org.joda.time.DateTimeZone denverTimeZone = org.joda.time.DateTimeZone.forID( "America/Denver" );
org.joda.time.DateTime invoiceZonedDateTime = invoiceStoredDateTime.toDateTime( denverTimeZone );
// Call method .withTimeAtStartOfDay() to set the time component to first moment of the day.
org.joda.time.DateTime dueDateInThirtyDays = invoiceZonedDateTime.plusDays( 30 ).withTimeAtStartOfDay();
org.joda.time.DateTime dueDateInOneMonth = invoiceZonedDateTime.plusMonths( 1 ).withTimeAtStartOfDay(); // Smart month calculation, aiming at same day number of month.
// Style – Specify a character of 'S' for short style, 'M' for medium, 'L' for long, and 'F' for full. First for date, second for time.
// A date or time may be omitted by specifying a style character '-'.
String dueDateAsString = org.joda.time.format.DateTimeFormat.forStyle("S-").withLocale( Locale.US ).print( dueDateInThirtyDays );
org.joda.time.DateTime dueDateInUtcForStorage = dueDateInThirtyDays.toDateTime( org.joda.time.DateTimeZone.UTC );
Show values on the console:
System.out.println( "date: " + date );
System.out.println( "invoiceZonedDateTime: " + invoiceZonedDateTime );
System.out.println( "dueDateInThirtyDays: " + dueDateInThirtyDays );
System.out.println( "dueDateInOneMonth: " + dueDateInOneMonth );
System.out.println( "dueDateAsString: " + dueDateAsString );
System.out.println( "dueDateInUtcForStorage: " + dueDateInUtcForStorage );
When run…
date: Thu Nov 28 13:39:05 PST 2013
invoiceZonedDateTime: 2013-11-28T14:39:05.125-07:00
dueDateInThirtyDays: 2013-12-28T00:00:00.000-07:00
dueDateInOneMonth: 2013-12-28T00:00:00.000-07:00
dueDateAsString: 12/28/13
dueDateInUtcForStorage: 2013-12-28T07:00:00.000Z

Related

processing last 10 working(business) days java

I tried to write a code which displays the days of the week for the last 10 dates.
Here's a part of the code:
Calendar cal = Calendar.getInstance();
for(int i=0; i<=9;i++) {
cal.add(Calendar.DATE, -i);
Date tday=cal.getTime();
SimpleDateFormat dy = new SimpleDateFormat("EEE");
String d9 = dy.format(tday);
System.out.println(d9);
}
Instead of showing all the last 10 days in an order it is displaying like this:
Thu
Wed
Mon
Fri
Mon
Wed
Thu
Thu
Wed
Mon
Fri
Where did I make the mistake?
try this.
boolean work = true;
int day = 0; // 0 = today, 1 = yesterday etc...
int subDay = 0; // subtract day
while (work){
Calendar cal = Calendar.getInstance(); // get current time
cal.add(Calendar.DAY_OF_WEEK, subDay); // subtract day
// working days are Mon, Tue, Wed, Thu, Fri. If we get saturdays or sundays, we want to skip that days,
// so we use if declaration
if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
subDay--;
continue;
}
Date tday=cal.getTime();
SimpleDateFormat dy = new SimpleDateFormat("EEE");
String d9 = dy.format(tday);
System.out.println("Day: " + day + " - " + d9);
day ++;
subDay--;
if (day >= 10){work=false;} // here we declara how much day we want to go back, and we break loop.
}
Avoid j.u.Date
The first mistake was using the java.util.Date and .Calendar classes bundled with Java. They are notoriously troublesome. Avoid them.
Use a decent date-time library. In Java that means either:
Joda-Time
java.time package in Java 8 (inspired by Joda-Time)
Both have their pros and cons.
Both offer a LocalDate class, which you need to represent a date-only without any time-of-day portion.
Date-Time Versus Text
The Question’s code mixes date-time values with their String representations. Better to get your work done using date-time values. Afterwards, separately, create String representations for presentation to the user. The idea is a separation of concerns, to make your code more clear and easier to test/debug.
Joda-Time
Example code in Java-Time.
You will have to specify which days of the week are business days.
Note the use of a time zone. The current date depends on your position on the globe (the time zone). A new day dawns in Paris earlier than in Montréal. If you omit the time zone, the JVM’s default is applied. Better to specify, even by calling getDefault(), than to rely on an implicit default.
First we gather a collection of desired date-time values.
int requiredCountOfDays = 10; // The Question demands 10 previous working days.
List<LocalDate> days = new ArrayList<LocalDate>( requiredCountOfDays ); // Collect desired LocalDate objects.
DateTimeZone timeZone = DateTimeZone.forID( "Europe/Paris" ); // Specify time zone by which to get current date.
LocalDate today = LocalDate.now( timeZone ); // Get the current date at this moment in specified time zone.
LocalDate localDate = today; // Define var to decrement for previous days.
while ( days.size() < requiredCountOfDays ) { // Loop until we fill the list (10 elements).
localDate = localDate.minusDays( 1 ); // Decrement to get previous day.
// Hard-code what days are business days vs weekend days.
boolean isWeekend = ( ( localDate.getDayOfWeek() == DateTimeConstants.SATURDAY ) || ( localDate.getDayOfWeek() == DateTimeConstants.SUNDAY ) ); // Hard-coding for weekend because it is easier to type than coding for the more numerous week days.
if ( !isWeekend ) { // If business day…
days.add( localDate ); // …collect this day.
}
}
Afterwards, we present those values in a localized String format.
List<String> daysOfWeek = new ArrayList<String>( days.size() ); // Collect the same number of LocalDate objects, rendered as Strings.
DateTimeFormatter formatter = DateTimeFormat.forPattern( "EEE" ); // Generate name of day-of-week, abbreviated.
for ( LocalDate day : days ) {
String dayOfWeek = formatter.print( day ); // Generate String representation.
daysOfWeek.add( dayOfWeek ); // Collect the string.
}
Dump to console…
System.out.println( "days: " + days );
System.out.println( "daysOfWeek: " + daysOfWeek );
When run…
days: [2014-06-18, 2014-06-17, 2014-06-16, 2014-06-13, 2014-06-12, 2014-06-11, 2014-06-10, 2014-06-09, 2014-06-06, 2014-06-05]
daysOfWeek: [Wed, Tue, Mon, Fri, Thu, Wed, Tue, Mon, Fri, Thu]

How can I convert a date in YYYYMMDDHHMMSS format to epoch or unix time?

EDIT: I have edited my question to include more information, I have tried many ways to do this already, asking a question on StackOverflow is usually my last resort. Any help is greatly appreciated.
I have a date (which is a Timestamp object) in a format of YYYYMMDDHHMMSS (e.g. 20140430193247). This is sent from my services to the front end which displays it in the format: date:'dd/MM/yyyy' using AngularJS.
How can I convert this into Epoch/Unix time?
I have tried the duplicate question that is linked to this, what I get returned is a different date.
I have also tried the following:
A:
//_time == 20140430193247
return _time.getTime()/1000; // returns 20140430193 - INCORRECT
B:
return _time.getTime(); // returns 20140430193247 (Frontend: 23/03/2608) - INCORRECT
C:
Date date = new Date();
//_time = 20140501143245 (i.e. 05/01/2014 14:32:45)
String str = _time.toString();
System.out.println("Time string is " + str);
//Prints: Time string is 2608-03-24 15:39:03.245 meaning _time.toString() cannot be used
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
try {
date = df.parse(str);
} catch (ParseException e) {
}
return date; // returns 20140501143245 - INCORRECT
D:
date = new java.sql.Date(_time.getTime());
return date; // returns 2608-03-24 - INCORRECT
The following shows the todays date correctly:
Date date = new Date();
return date; // returns 1398939384523 (Frontend: 01/05/2014)
Thanks
I got the answer after quite a while of trying different ways. The solution was pretty simple - to parse the time to a string as toString() didn't work.
Date date;
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
try {
date = df.parse(String.valueOf(_time.getTime()));
} catch (ParseException e) {
throw new RuntimeException("Failed to parse date: ", e);
}
return date.getTime();
tl;dr
LocalDateTime
.parse(
"20140430193247" ,
DateTimeFormatter.ofPattern( "uuuuMMddHHmmss" )
)
.atOffset(
ZoneOffset.UTC
)
.toEpochSecond()
java.time
Parse your input string as a LocalDateTime as it lacks an indicator of offset-from-UTC or time zone.
String input = "20140430193247" ;
DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuuMMddHHmmss" ) ;
LocalDateTime ldt = LocalDateTime.parse( input , f ) ;
Now we have a date with time-of-day, around half-past 7 PM on April 30 of 2014. But we lack the context of offset/zone. So we do not know if this was 7 PM in Tokyo Japan or 7 PM in Toledo Ohio US, two different moments that happened several hours apart.
To determine a moment, you must know the intended offset/zone.
If you know for certain that an offset of zero, or UTC itself, was intended, apply the constant ZoneOffset.UTC to get an OffsetDateTime.
OffsetDateTime odt = ldt.atOffset( ZoneOffset.UTC ) ;
How can I convert this into Epoch/Unix time?
Do you mean a count of seconds or milliseconds since the first moment of 1970 in UTC?
For a count of whole seconds since 1970-01-01T00:00Z, interrogate the OffsetDateTime object.
long secondsSinceEpoch = odt.toEpochSecond() ;
For milliseconds, extract a Instant object. An Instant represents a moment in UTC, and is the basic building-block class of java.time. Then interrogate for the count.
Instant instant = odt.toInstant() ;
long millisSinceEpoch = instant.toEpochMilli() ;

Convert miliseconds to date and time and format by locale

How can I convert milliseconds to a time and date string and format it correctly like the user expects it to be?
I did the following:
((SimpleDateFormat)DateFormat.getDateInstance(DateFormat.DEFAULT,Locale.getDefault())).format(new Date(Long.parseLong(timeInMilliseconds)));
Which seems to work, but I only get the date with this method.
Edit:
To clearify, I need to get the time/date pattern from system somehow to give each user his common format
Now I combined your solutions with mine and it seems to work like I expect.
private String getFormattedDateTimeString(Context context, String timeInMilliseconds) {
SimpleDateFormat dateInstance = (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.DEFAULT, Locale.getDefault());
SimpleDateFormat timeInstance = (SimpleDateFormat) DateFormat.getTimeInstance(DateFormat.DEFAULT, Locale.getDefault());
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(Long.parseLong(timeInMilliseconds));
String date = dateInstance.format(calendar.getTime());
String time = timeInstance.format(calendar.getTime());
return date + " " + time;
}
Why the hell do I get downvotes for this question???
All the other answers are missing the point that the string representation of the date-time needs to be localized.
Joda-Time
The Joda-Time 2.3 library makes this work much easier.
Joda-Time leverages a java.util.Locale to determine proper formatting of a date-time's string representation. The DateTimeFormat class offers an option for "style" pattern as a way of generating a DateTimeFormatter. You specify a two character style pattern. The first character is the date style, and the second character is the time style. Specify a character of 'S' for short style, 'M' for medium, 'L' for long, and 'F' for full. A date or time may be omitted by specifying a style character '-'.
If you do not specify a Locale or time zone, the JVM's default will be used.
Locale
To create a java.util.Locale, you need:
Language code (either, see combined list)
ISO 639 alpha-2
ISO 639 alpha-3
Country Code (either)
ISO 3166 alpha-2 country code
UN M.49 numeric-3 area code
Example Code
// Simulate input.
long millisecondsSinceEpoch = DateTime.now().getMillis();
// Proceed with a 'long' value in hand.
DateTime dateTimeUtc = new DateTime( millisecondsSinceEpoch, DateTimeZone.UTC );
DateTimeZone timeZone = DateTimeZone.forID( "Asia/Riyadh" );
DateTime dateTimeRiyadh = dateTimeUtc.withZone( timeZone );
// 'ar' = Arabic, 'SA' = Saudi Arabia.
java.util.Locale locale = new Locale( "ar", "SA" ); // ( language code, country code );
DateTimeFormatter formatter = DateTimeFormat.forStyle( "FF" ).withLocale( locale ).withZone( timeZone );
String output = formatter.print( dateTimeUtc );
Dump to console…
System.out.println( "millisecondsSinceEpoch: " + millisecondsSinceEpoch );
System.out.println( "dateTimeUtc: " + dateTimeUtc );
System.out.println( "dateTimeRiyadh: " + dateTimeRiyadh );
System.out.println( "output: " + output );
When run…
millisecondsSinceEpoch: 1392583624765
dateTimeUtc: 2014-02-16T20:47:04.765Z
dateTimeRiyadh: 2014-02-16T23:47:04.765+03:00
output: 16 فبراير, 2014 AST 11:47:04 م
Leaving your code as is, just change:
Instead of
getDateInstance
try
getDateTimeInstance
Or, you'd better use:
SimpleDateFormat fmt = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss");
String datetime = fmt.format(cal.getTimeInMillis());
Use this...
String dateFormat = "dd/MM/yyyy hh:mm:ss.SSS";
// Create a DateFormatter object for displaying date in specified format.
DateFormat formatter = new SimpleDateFormat(dateFormat);
// Create a calendar object that will convert the date and time value in milliseconds to date.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(milliSeconds);
String formatedDate = formatter.format(calendar.getTime());
Try this
SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");// you can rearange it as you like
cal.setTimeInMillis(timeInMilliseconds); //convert the time in milli to a date
String date = format.format(cal.getTime());// here you get your time formated
Why on earth do you want to use a calendar object???? It's just a waste of resources.
// Create a DateFormatter object for displaying date in specified format.
DateFormat myDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
String formatedDate = myDateFormat.format(new Date(timeInMilliseconds));

String Date Java

// Im new to java programming
I have a String object that represents a date/time in this format : "2013-06-09 14:20:00" (yyyy-MM-dd HH:mm:ss)
I want to convert it to a Date object so i can perform calculations on it but im confused on how to do this.
I tried :
String string = "2013-06-09 14:20:00";
Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(string);
System.out.println(date);
//Prints Mon Dec 31 00:00:00 GMT 2012
Any help appreciated
Ok so I have now updated my code to as follows i'm getting the correct date/time now when I print the date but is this the correct implementation :
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String string = "2013-06-09 14:20:00";
Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(string);
System.out.println(dateFormat.format(date));
//prints 2013-06-09 14:20:00
Thx to everyone that's answered/commented thus far
The format is wrong. Use this instead:
"yyyy-dd-MM HH:mm:ss"
Indeed your last program version is ok, except you don't need to declare the SimpleDateFormat twice. Simply:
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String string = "2013-06-09 14:20:00";
Date date = dateFormat.parse(string);
System.out.println(dateFormat.format(date));
String string = "2013-06-09 14:20:00";
and the DATE object format is "yyyy-dd-MM HH:mm:ss"
You can get Date,Day,month and many more by using Date object which is present in
java.util.Date package , like as follows.
Date d = new Date(string);
This will call constructor of Date object for which you are passing 'string' variable which contains date.
d.getDay(); // retrieve day on that particular day
d.getDate(); // retrieve Date
and many more are avaiable like this.
Using java.util.Date
The answer by zzKozak is correct. Well, almost correct. The example code omits required exception handling. Like this…
java.text.DateFormat dateFormat = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String string = "2013-06-09 14:20:00";
Date date = null;
try {
date = dateFormat.parse(string);
} catch ( ParseException e ) {
e.printStackTrace();
}
System.out.println("date: " + dateFormat.format(date));
Don't Use java.util.Date!
Avoid using java.util.Date & Calendar classes bundled with Java. They are notoriously bad in both design and implementation.
Instead use a competent date-time library. In Java that means either:
The third-party open-source Joda-Time
In the forthcoming Java 8, the new java.time.* classes defined by JSR 310 and inspired by Joda-Time.
Time Zone
Your question and code fail to address the issue of time zones. If you ignore time zones, you'll get defaults. That may cause unexpected behaviors when deployed in production. Better practice is to always specify a time zone.
Formatter
If you replace a space with a 'T' per the standard ISO 8601 format, then you can conveniently feed that string directly to a constructor of a Joda-Time DateTime instance.
If you must use that string as-is, then define a formatter to specify that format. You can find many examples of that here on StackOverflow.com.
Example Code
Here is some example code using Joda-Time 2.3, running in Java 7.
I arbitrarily chose a time zone of Montréal.
// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.
// import org.joda.time.*;
// import org.joda.time.format.*;
// Specify a time zone rather than rely on default.
// Necessary to handle Daylight Saving Time (DST) and other anomalies.
DateTimeZone timeZone = DateTimeZone.forID( "America/Montreal" );
DateTime dateTime = new DateTime( "2013-06-09T14:20:00", timeZone ); // Or pass DateTimeZone.UTC as time zone for UTC/GMT.
System.out.println( "dateTime: " + dateTime );
When run…
dateTime: 2013-06-09T14:20:00.000-04:00

check my Calendar date parameter is current date

I just wanted to find date parameter is current date (yyyy-MM-dd) without using simpledateformater or any date to string convertion and then find is equals.
specifiedDate=2012-12-20
currentDate=2012-12-21
specifiedDate == currentDate
to be simple i dont want time (i.e HH:mm:S) not to be included while validating
i have tried something like
public boolean isCurrentDate(Calendar date){
Calendar currentDate = Calendar.getInstance().getTime();
if (currentDate.getDate()==(date.getTime().getDate())
&& currentDate.getMonth()==(date.getTime().getMonth())
&& currentDate.getYear()==(date.getTime().getYear()) )
{
return true;
}
return false;
}
please suggest a better way or if any libraries already available for this !!
What about setting time fields to 0 before comparing
currentDate.set(Calendar.HOUR_OF_DAY, 0);
currentDate.set(Calendar.MINUTE, 0);
currentDate.set(Calendar.SECOND, 0);
currentDate.set(Calendar.MILLISECOND, 0);
Try this if you want to do only
1) Using strings
String s1 = new String("2012-01-27");
String s2 = new String("2011-01-28");
System.out.println(s1.compareTo(s2));
The result will be TRUE if s1 is "bigger" than s2 in lexicographical way and it's what you need. To get more info read javadoc for compareTo() method.
2) Using Joda Time
Using Joda Time lib you can acheive as below
DateTime first = ...;
DateTime second = ...;
LocalDate firstDate = first.toLocalDate();
LocalDate secondDate = second.toLocalDate();
return firstDate.compareTo(secondDate);
I prefer second option
If you are using calendar
public static boolean isSameDay(Calendar cal1, Calendar cal2) {
if (cal1 == null || cal2 == null) {
throw new IllegalArgumentException("The dates must not be null");
}
return (cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA) &&
cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) &&
cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR));
}
public static boolean isToday(Calendar cal) {
return isSameDay(cal, Calendar.getInstance());
}
If you are using Date
public static boolean isSameDay(Date date1, Date date2) {
if (date1 == null || date2 == null) {
throw new IllegalArgumentException("The dates must not be null");
}
Calendar cal1 = Calendar.getInstance();
cal1.setTime(date1);
Calendar cal2 = Calendar.getInstance();
cal2.setTime(date2);
return isSameDay(cal1, cal2);
}
public static boolean isToday(Date date) {
return isSameDay(date, Calendar.getInstance().getTime());
}
Your last line && currentDate.getYear()==(date.getMonth()) ) appears to be comparing the year and month not the year and year. Could this be your issue?
Try this:
currentDate.set(Calendar.DATE, 0);
Time Zone
The example code in your question ignores the crucial issue of time zone. The date, that is the beginning and ending points of a day, is defined by a time zone.
Both java.util.Calendar and java.util.Date have no time zone assigned. They represent a date and a time in UTC/GMT.
So you need to apply a desired time zone, relevant to the context of your app & data. That means you need a decent date-time library. Something other than java.util.Date/Calendar, java.text.SimpleDateFormat and their sibling classes, as they are notoriously troublesome. Use either Joda-Time or the new java.time.* package bundled with Java 8 (inspired by Joda-Time, defined by JSR 310).
Note the use of the method withTimeAtStartOfDay. That gets the first moment of the day. That is usually the time 00:00:00 but not always. Daylight Saving Time (DST) or other anomalies may produce a different time. That method smartly handles such issues.
Today = Span of Time
Technically, when working with date-time values, a particular "date" is actually a span of time. The most common and generally useful way to define that span is "half-open" where the beginning is inclusive and the ending is exclusive. That means, for current date, we want the first moment of today (inclusive) to the first moment of tomorrow (exclusive). Then we ask if the target date-time falls within that span.
There are other ways to get this job done. I'm showing this approach because it is applies to situations beyond the question of "today".
Joda-Time
Joda-Time offers three classes for defining a span of time: Interval, Period, and Duration.
Example Code
Setup our input data, a Calendar object.
// Create a Calendar object to simulate input.
java.util.Date date = DateTime.now().minusDays( 3 ).toDate();
java.util.Calendar cal = java.util.Calendar.getInstance();
cal.setTime( date );
Define "today" as a span of time, and see if target date-time falls within that span.
DateTimeZone timeZone = DateTimeZone.forID( "Asia/Kolkata" );
DateTime dateTimeInQuestion = new DateTime( cal.getTimeInMillis(), timeZone );
DateTime now = new DateTime( timeZone );
Interval today = new Interval( now.withTimeAtStartOfDay(), now.plusDays( 1 ).withTimeAtStartOfDay() );
boolean isDateTimeInQuestionInInterval = today.contains( dateTimeInQuestion );
Dump to console…
System.out.println( "cal: " + cal.getTime() );
System.out.println( "dateTimeInQuestion: " + dateTimeInQuestion );
System.out.println( "now: " + now );
System.out.println( "today: " + today );
System.out.println( "isDateTimeInQuestionInInterval: " + isDateTimeInQuestionInInterval );
When run…
cal: Wed Feb 12 22:46:04 PST 2014
dateTimeInQuestion: 2014-02-13T12:16:04.369+05:30
now: 2014-02-16T12:16:04.497+05:30
today: 2014-02-16T00:00:00.000+05:30/2014-02-17T00:00:00.000+05:30
isDateTimeInQuestionInInterval: false

Categories