I asked this question before however I did not get the answer that I expected. Therefore I opened this new question.
My try:
String fileName = "placements.csv";
try {
// Assume default encoding.
FileWriter fileWriter = new FileWriter(fileName);
// Always wrap FileWriter in BufferedWriter.
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
// First row, write the head of the csv file.
bufferedWriter.write(FILE_HEADER);
bufferedWriter.newLine();
// Increase the begin date 1 seconds until test end date.
int seconds = 0;
Calendar cal = Calendar.getInstance();
cal.setTime(beginDate);
for (int j = 0; j < convertedDifference; j++) {
for (Job currentJob : NEHCalculator.sequenceOrderListofJobs()) {
int times = (int) ((convertedDifference / currentJob.getInterval()) * testDevices());
for (int i = 0; i < currentJob.getNeededTestTime() * times; i++) {
cal.add(Calendar.SECOND, 1);
beginDate.getTime();
// write the test date
bufferedWriter.write(String.valueOf(cal.getTime()));
bufferedWriter.write(";");
bufferedWriter.write(String.valueOf(seconds));
bufferedWriter.write(";");
bufferedWriter.write(currentJob.getJobname());
bufferedWriter.write(";");
bufferedWriter.newLine();
}// end of currentjob loop
seconds++;
}// end first for loop
}
bufferedWriter.close();// Always close files.
} catch (IOException ex) {
System.out.println("Error writing to file '" + fileName);
}
}
First for loop for (int j = 0; j < convertedDifference; j++) : I restrict my program until test end date. If I enter 1 hour test time, i expected to see number 0 until 3599 seconds.
Second for loop for (Job currentJob : NEHCalculator.sequenceOrderListofJobs()): I want to test every devices in the list.
For third for loop for (int i = 0; i < currentJob.getNeededTestTime() * times; i++): I expected to test first job currentJob.getNeededTestTime() * times times.
For example I have 1 hour test time and interval of fist job is 15 min and it tests 2 devices and test needed time 2 seconds. So the output must be:
Mon Feb 22 12:59:59 CET 2016;0;WAF5-H;
Mon Feb 22 13:00:00 CET 2016;1;WAF5-H;
Mon Feb 22 13:00:01 CET 2016;2;WAF5-H;
Mon Feb 22 13:00:02 CET 2016;3;WAF5-H;
Mon Feb 22 13:00:03 CET 2016;4;WAF5-H;
Mon Feb 22 13:00:04 CET 2016;5;WAF5-H;
Mon Feb 22 13:00:05 CET 2016;6;WAF5-H;
Mon Feb 22 13:00:06 CET 2016;7;WAF5-H;
Mon Feb 22 13:00:07 CET 2016;8;WAF5-H;
Mon Feb 22 13:00:08 CET 2016;9;WAF5-H;
Mon Feb 22 13:00:09 CET 2016;10;WAF5-H;
Mon Feb 22 13:00:10 CET 2016;11;WAF5-H;
Mon Feb 22 13:00:11 CET 2016;12;WAF5-H;
Mon Feb 22 13:00:12 CET 2016;13;WAF5-H;
Mon Feb 22 13:00:13 CET 2016;14;WAF5-H;
Mon Feb 22 13:00:14 CET 2016;15;WAF5-H; then it will continue with second job until end of the job list.
However the output of my code is:
Mon Feb 22 12:59:59 CET 2016;0;WAF5-H;
Mon Feb 22 13:00:00 CET 2016;0;WAF5-H;
Mon Feb 22 13:00:01 CET 2016;0;WAF5-H;
Mon Feb 22 13:00:02 CET 2016;0;WAF5-H;
Mon Feb 22 13:00:03 CET 2016;0;WAF5-H;
Mon Feb 22 13:00:04 CET 2016;2;WAF5-H;
Mon Feb 22 13:00:05 CET 2016;2;WAF5-H;
Mon Feb 22 13:00:06 CET 2016;2;WAF5-H;
Mon Feb 22 13:00:07 CET 2016;2;WAF5-H;
Mon Feb 22 13:00:08 CET 2016;2;WAF5-H; until Mon Feb 22 17:55:08 CET 2016;7082;WAF5-H;
It is completely wrong and I spend really so soo much time but I failed with this task. Could someone please help me.
Best regards,
What about changing this code
bufferedWriter.write(String.valueOf(seconds));
for this one:
bufferedWriter.write(String.valueOf(cal.get(Calendar.SECOND)));
I think it might work
Related
I am new to java and I am trying to get all the 7th day in the year 2009.
I am a bit confused about how to go about it. Below is my code
public class Main {
public static void main(String[] args) {
System.out.println("WELCOME TO MY CALENDER CLASS");
Calendar calendar = Calendar.getInstance();
calendar.set(DAY_OF_MONTH,7);
calendar.set(Calendar.YEAR,2009);
for(int i =1; i <= 12; i++){
calendar.set(DAY_OF_MONTH,i);
System.out.println(calendar.getTime());
}
}
}
Update: This below is my result
Sun Mar 01 23:41:14 GMT 2009
Mon Mar 02 23:41:14 GMT 2009
Tue Mar 03 23:41:14 GMT 2009
Wed Mar 04 23:41:14 GMT 2009
Thu Mar 05 23:41:14 GMT 2009
Fri Mar 06 23:41:14 GMT 2009
Sat Mar 07 23:41:14 GMT 2009
Sun Mar 08 23:41:14 GMT 2009
Mon Mar 09 23:41:14 GMT 2009
Tue Mar 10 23:41:14 GMT 2009
Wed Mar 11 23:41:14 GMT 2009
Thu Mar 12 23:41:14 GMT 2009
OK, assuming you are starting from 1st of January here is a simple example for your cause. I hope Java1.8 code is clear to you.
public static void main(String[] args) {
// create two localdate start of a year instances, one for current year and one for next year, 2009 and 2010 respectively
LocalDate thisYear = LocalDate.of(2009, Month.JANUARY, 1);
LocalDate nextYear = LocalDate.of(2010, Month.JANUARY, 1);
// used only for counting number of every seventh day in a year
int i=0;
// while we are not in the next year, 2010
while (thisYear.isBefore(nextYear)) {
i++;
// print current date
System.out.println(i+" " + thisYear.toString());
// add a week or seven days to our thisYear instance and loop thru again
thisYear = thisYear.plusWeeks(1);
}
}
The problem with your code is that in the for loop you set the day and not the month for the Calendar object.
So change to this:
for(int i = 0; i < 12; i++){
calendar.set(Calendar.MONTH, i);
System.out.println(calendar.getTime());
}
The loop starts from 0 and goes up to 11 because the months are 0 based.
If you can use LocalDate then your code would be much simpler and more efficient:
System.out.println("WELCOME TO MY CALENDER CLASS");
LocalDate date;
for(int i = 1; i <= 12; i++){
date = LocalDate.of(2009, Month.of(i), 7);
System.out.println(date.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL)));
}
My requirement is determining the next date based on frequency of schedule.
So if frequency is DAILY and the first date is 25-Oct-2015 23:59:59,
the next duedate should be exactly 24 hours apart ie 26-Oct-2015 23:59:59
Calendar.add(int field, int amount) seems to be taking care of the same
Eg:
DAILY frequency -- calendar.add(Calendar.DATE, 1);
WEEKLY frequency -- calendar.add(Calendar.DATE, 7);
MONTHLY frequency -- calendar.add(Calendar.MONTH, 1);
The following is the code abstract of the same:
public static void main(String[] args) {
Calendar calendar = new GregorianCalendar();
for(int i=0;i<10;i++) {
calendar.add(Calendar.DATE, 1);
System.out.println(calendar.getTime());
}
}
==================================================
Thu Oct 29 17:17:26 IST 2015 -- in all cases diff is 24 hrs
Fri Oct 30 17:17:26 IST 2015
Sat Oct 31 17:17:26 IST 2015
Sun Nov 01 17:17:26 IST 2015
Mon Nov 02 17:17:26 IST 2015
Tue Nov 03 17:17:26 IST 2015
Wed Nov 04 17:17:26 IST 2015
Thu Nov 05 17:17:26 IST 2015
Fri Nov 06 17:17:26 IST 2015
In case of DAILY frequency and server being in Eastern time (EDT), a few anomaly was ocuuring with add()
As of Nov-1, DST settings change the same are reflected in add
public static void main(String[] args) {
Calendar calendar = new GregorianCalendar();
calendar.setTimeZone(TimeZone.getTimeZone("US/Eastern"));
for(int i=0;i<10;i++) {
calendar.add(Calendar.DATE, 1);
System.out.println(calendar.getTime());
}
}
-------------------------------------
Wed Oct 28 17:18:14 IST 2015
Thu Oct 29 17:18:14 IST 2015
Fri Oct 30 17:18:14 IST 2015
Sat Oct 31 17:18:14 IST 2015
Sun Nov 01 18:18:14 IST 2015 -- here diff is of 24 + 1 hr
Mon Nov 02 18:18:14 IST 2015
Tue Nov 03 18:18:14 IST 2015
Wed Nov 04 18:18:14 IST 2015 -- else everywhere diff is 24 hours
Thu Nov 05 18:18:14 IST 2015
Fri Nov 06 18:18:14 IST 2015
In case by first date is 25-Oct-2015 23:59:59, in this case, the extra 1 hour shift is causing anomaly as after
31-Oct-2015 23:59:59,
the next date is 2-Nov-2015 00:59:59
Further observing the Code, found out that
// The rest of the fields (week, day or AM_PM fields)
// require time zone offset (both GMT and DST) change
// adjustment.
Actually the server is in EDT, where I'm getting following I/O relation.
I merely tried to debug it on my local instance which is in IST.
public static void main(String[] args) {
Calendar calendar = new GregorianCalendar();
for(int i=0;i<10;i++) {
calendar.add(Calendar.DATE, 1);
System.out.println(calendar.getTime());
}
}
Wed Oct 28 08:09:48 EDT 2015
Thu Oct 29 08:09:48 EDT 2015
Fri Oct 30 08:09:48 EDT 2015
Sat Oct 31 08:09:48 EDT 2015
Sun Nov 01 08:09:48 EST 2015
Mon Nov 02 08:09:48 EST 2015
Tue Nov 03 08:09:48 EST 2015
Wed Nov 04 08:09:48 EST 2015
Thu Nov 05 08:09:48 EST 2015
Fri Nov 06 08:09:48 EST 2015
What should be a reliable way of using a library to ensure that my dates generated are in proper sequence.
Try the below code. The below code doesn't simpley add 1 to DAY or MONTH. It adds milliseconds for a day in order to get the proper result.
public static void main(String[] args) {
DateFormat formatter = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy");
Calendar calendar = Calendar.getInstance();
formatter.setTimeZone(TimeZone.getTimeZone("US/Eastern"));
calendar.setTimeInMillis(System.currentTimeMillis());
long time = calendar.getTimeInMillis();
for(int i=0;i<10;i++) {
System.out.println(formatter.format(calendar.getTime()));
time = time + 86400000;
calendar.setTimeInMillis(time);
}
}
Definitely formatting of the date can be changed by changing the line -
new SimpleDateFormat("E MMM dd hh:mm:ss z yyyy");
It gives output -
Di Okt 27 08:15:59 EDT 2015
Mi Okt 28 08:15:59 EDT 2015
Do Okt 29 08:15:59 EDT 2015
Fr Okt 30 08:15:59 EDT 2015
Sa Okt 31 08:15:59 EDT 2015
So Nov 01 07:15:59 EST 2015
Mo Nov 02 07:15:59 EST 2015
Di Nov 03 07:15:59 EST 2015
Mi Nov 04 07:15:59 EST 2015
Do Nov 05 07:15:59 EST 2015
I think the anomaly is because, the daylight is ending for EDT zone on that day(31st October night).
NO such problem is occurring for IST as India do not have daylight saving.
I have a string which contains my mail details such as
GitHub Sat Jun 01 13:32:02 IST 2013
eBay Mon Jun 03 17:37:40 IST 2013
YouTube Tue Jun 04 00:18:50 IST 2013
YouTube Sat Jun 08 01:20:47 IST 2013
eBay Sat Jun 08 13:19:22 IST 2013
eBay Sat Jun 08 13:17:53 IST 2013
eBay Mon Jun 10 15:43:01 IST 2013
YouTube Mon Jun 10 15:47:02 IST 2013
eBay Wed Jun 12 11:10:15 IST 2013
eBay Wed Jun 12 19:25:50 IST 2013
eBay Thu Jun 13 17:22:14 IST 2013
eBay Thu Jun 13 18:09:18 IST 2013
Clark University Thu Jun 13 19:30:09 IST 2013
is there any way to get number of times eBay ,Youtube or anything has occured so that i can have a number of mails received from a particular person??
You can split the string using String tokenizer and match the value of first token.
If eBay is always going to be the first word then, you can use startsWith() method of String class.
try to use regular expressions with grouping of data.
This solution supposes two things:
names of days cannot appear in the first part;
each entry is separated by a newline:
[extra line to fix SO bug with list items and code extracts]
private static final Set<String> DAYS = new HashSet<String>(Arrays.asList(
"Mon ", "Tue ", "Wed ", "Thu ", "Fri ", "Sat ", "Sun "
));
public int nrMatches(final String mailList, final String sender)
{
int ret = 0;
int index;
for (final String line: mailList.split("\r?\n"))
for (final String day: DAYS) {
index = line.indexOf(day);
if (index == -1)
continue;
if (line.subString(0, index).trim().equals(sender))
ret++;
}
return ret;
}
Here is a simple test: I am trying to increment current timestamp by 1 second in a loop. The output is not what I expect.
public class TimeTest {
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
for (int i = 0; i < 10; i++) {
cal.add(Calendar.SECOND, i);
System.out.println("Updated = " + cal.getTime());
}
}
}
Instead of neat 1 second increments, I get increments from anywhere between 5 seconds to 1 second.
Updated = Mon May 13 15:12:45 PDT 2013
Updated = Mon May 13 15:12:46 PDT 2013
Updated = Mon May 13 15:12:48 PDT 2013
Updated = Mon May 13 15:12:51 PDT 2013
Updated = Mon May 13 15:12:55 PDT 2013
Updated = Mon May 13 15:13:00 PDT 2013
Updated = Mon May 13 15:13:06 PDT 2013
Updated = Mon May 13 15:13:13 PDT 2013
Updated = Mon May 13 15:13:21 PDT 2013
Updated = Mon May 13 15:13:30 PDT 2013
You want to add 1 instead of i on each for loop iteration.
cal.add(Calendar.SECOND, 1);
cal.add(Calendar.SECOND, 1);
Simply increment the calendar by 1 second instead of i seconds
I want to schedule a daily job at 23:59:59 only in weekdays (monday - friday).
i use this cron expression
"59 59 23 ? * MON-FRI",
but the output has tripe value for monday
Wed Aug 29 23:59:59 ICT 2012
Thu Aug 30 23:59:59 ICT 2012
Fri Aug 31 23:59:59 ICT 2012
Mon Sep 03 23:59:59 ICT 2012
Mon Sep 03 23:59:59 ICT 2012
Mon Sep 03 23:59:59 ICT 2012
Tue Sep 04 23:59:59 ICT 2012
Wed Sep 05 23:59:59 ICT 2012
Thu Sep 06 23:59:59 ICT 2012
Fri Sep 07 23:59:59 ICT 2012
is the expression wrong? need help.
i'm getting this output by loop through specific date, here the code
`try {
CronExpression ce = new CronExpression(59 59 23 ? * MON-FRI);
Calendar start = Calendar.getInstance();
start.setTime(new Date());
Calendar end = Calendar.getInstance();
Date endDate = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy").parse("Fri Sep 29 23:59:59 ICT 2012");
end.setTime(endDate);
for (; !start.after(endDate); start.add(Calendar.DATE, 1)) {
Date current = start.getTime();
System.out.println(ce.getNextValidTimeAfter(current));
}
} catch (ParseException ex) {
Logger.getLogger(HelloJob.class.getName()).log(Level.SEVERE, null, ex);
}
}`
The problem isn't in you rule or in Quartz, it's OK and you may use it.
The problem is in your test code.
for (; !start.after(endDate); start.add(Calendar.DATE, 1)) {
Date current = start.getTime();
System.out.println(ce.getNextValidTimeAfter(current));
}
You're not iterating on valid dates but on all days between startDate and endDate.
The loop content is called for invalid days too and for each of those 2 invalid days the "next valid time" after current date is monday. So you have thrice monday, that's perfectly logic.
Hence your log.