I want to print the 31 business working day by comparing two days.it should print date in valid format
Clash Royale CLAN TAG#URR8PPP
I want to print the 31 business working day by comparing two days.it should print date in valid format
I am taking start date and end date. I am iterating from start date to end date based on working days once I cross 30th day I am breaking that condition and I want to print the exact 30th day. While I am printing the 31st date it's not giving valid format.it's giving below output:
31st date isjava.util.GregorianCalendar[time=1103308200000,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Calcutta",offset=19800000,dstSavings=0,useDaylight=false,transitions=6,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2004,MONTH=11,WEEK_OF_YEAR=51,WEEK_OF_MONTH=3,DAY_OF_MONTH=18,DAY_OF_YEAR=353,DAY_OF_WEEK=7,DAY_OF_WEEK_IN_MONTH=3,AM_PM=0,HOUR=0,HOUR_OF_DAY=0,MINUTE=0,SECOND=0,MILLISECOND=0,ZONE_OFFSET=19800000,DST_OFFSET=0] 31
below is my code:
import java.text.SimpleDateFormat;
import java.util.Calendar;
import org.openqa.selenium.WebDriver;
public class DateDifferences_Validation
static WebDriver driver;
public static void main(String args)
String startdate = "06/09/2004";
String enddate = "18/12/2004";
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
try
Calendar start = Calendar.getInstance();
start.setTime(sdf.parse(startdate));
Calendar end = Calendar.getInstance();
end.setTime(sdf.parse(enddate));
int workingDays = 0;
while(!start.after(end))
int day = start.get(Calendar.DAY_OF_WEEK);
if ((day != Calendar.SATURDAY) && (day != Calendar.SUNDAY))
workingDays++;
start.add(Calendar.DATE, 1);
if(workingDays>30)
System.out.println("31st date is"+ end);
break;
System.out.println(workingDays);
catch(Exception e)
e.printStackTrace();
SimpleDateFormat
i did like this also.. String d=sdf.format(end); System.out.println("31st date is"+ d); it's throwing an error Cannot format given Object as a Date
– Mani
Aug 6 at 14:21
What is the valid format?
end.getTime()
will give you out put as Sat Dec 18 00:00:00 CET 2004
– NullPointer
Aug 6 at 14:36
end.getTime()
Sat Dec 18 00:00:00 CET 2004
FYI, the terribly troublesome old date-time classes such as
java.util.Date
, java.util.Calendar
, and java.text.SimpleDateFormat
are now legacy, supplanted by the java.time classes built into Java 8 and later. See Tutorial by Oracle.– Basil Bourque
Aug 6 at 20:45
java.util.Date
java.util.Calendar
java.text.SimpleDateFormat
3 Answers
3
It's not working because the end
object is a calendar, not a date. You need to obtain a Date object by calling end.getTime()
which can then be formatted:
end
end.getTime()
if(workingDays>30)
// Raw display
System.out.println("31st date is "+ end.getTime());
// Formatted display
System.out.println("31st date is "+ sdf.format( end.getTime() ));
break;
Output:
31st date is Sat Dec 18 00:00:00 CET 2004
31st date is 18/12/2004
31
but, there i want to print 31 date only.... not end date.It's giving output as end date.
– Mani
Aug 6 at 14:39
Just print the current date (meaning the current value of start) when workingDays=30.
– StephaneM
Aug 6 at 14:46
it's working now .... if(workingDays>30) start.getTime(); System.out.println("31st date is "+ start.getTime()); System.out.println("31st date is "+ sdf.format( start.getTime() )); break;
– Mani
Aug 6 at 14:48
it's working now,instead of passing end object we need to pass start object.`
if(workingDays>30)
start.getTime();
System.out.println("31st date is "+ start.getTime());
System.out.println("31st date is "+ sdf.format( start.getTime() ));
break;`
op is:
31st date is Tue Oct 19 00:00:00 IST 2004
31st date is 19/10/2004
31
java.time
Much easier with the java.time classes that supplanted the troublesome old classes seen in the Question.
Your rules are convoluted, but here is some untested example code that might be close to what you seek.
ISO 8601
When representing date-time values as text, use standard ISO 8601 formats. For a date-only value, that would be YYYY-MM-DD.
The java.time classes use ISO 8601 formats by default when parsing/generating strings.
LocalDate
LocalDate
LocalDate start = LocalDate.parse( "2004-09-06" ) ;
LocalDate stop = LocalDate.parse( "2004-12-18" ) ;
Collect our resulting dates.
List< LocalDate > dates = new ArrayList<>( 31 ) ; // Set initial capacity when known.
We can skip to next non-weekend-day either with our own code using an EnumSet
with the DayOfWeek
enum. We can then ask if the set contains the day-of-week of each incremented date.
EnumSet
DayOfWeek
EnumSet weekend = EnumSet.of( DayOfWeek.SATURDAY , DayOfWeek.SUNDAY ) ;
Loop those dates. In date-time handling, the Half-Open approach to defining a span-of-time is usually best. This means the beginning is inclusive while the ending is exclusive.
LocalDate ld = start ;
while( ld.isBefore( stop ) && ( dates.size() <= 31 ) ) // If within date-range, and not over 31 days collected.
if( dates.size() != 30 )
if( weekend.contains( ld.getDayOfWeek() )
// Do nothing. Let the date get incremented below.
else
dates.add( ld ) ;
else // if 30th, add, ignoring rule about skipping weekend.
dates.add( ld ) ;
// Setup the next loop.
ld = ld.plusDays( 1 ) ;
Tip: Instead of making an EnumSet
for weekend, you can use the TemporalAdjuster
implementation nextWorkingDay()
found in the ThreeTen-Extra project.
EnumSet
TemporalAdjuster
nextWorkingDay()
LocalDate nextWorkingDay = myLocalDate.with( org.threeten.extra.Temporals.nextWorkingDay() ) ;
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
java.util.Date
Calendar
SimpleDateFormat
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes.
java.sql.*
Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.
Interval
YearWeek
YearQuarter
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Use the
SimpleDateFormat
to display the date.– Arnaud Denoyelle
Aug 6 at 14:17