Create one file per week
I have a java program which is gonna be manually run every day.
Now I want to create an excel file per wek to write in it all tasks of the week
I know how to create a file every day like this:
if(!IoUtils.fileExist("indicators-" + IoUtils.getCurrentDate() + ".xls")){
IoUtils.createIndicFile("indicators-" + IoUtils.getCurrentDate() + ".xls");
}
else IoUtils.INDIC_FILEPATH = "indicators-" + IoUtils.getCurrentDate() + ".xls";
Here is the function which give me the current date in a specific format:
// IoUtils class
public static String getCurrentDate(){
LocalDateTime ldt = LocalDateTime.now();
return DateTimeFormatter.ofPattern("dd-MM-yyyy", Locale.ENGLISH).format(ldt);
}
So how can I change this to only create a file per week ?
I would also like to have the month and the number of the week in the filename like this:
// first monday of january 2018
name = "indicators-week1-01-2018"
// second monday of january 2018
name = "indicators-week2-01-2018"
Thank you
java
|
show 2 more comments
I have a java program which is gonna be manually run every day.
Now I want to create an excel file per wek to write in it all tasks of the week
I know how to create a file every day like this:
if(!IoUtils.fileExist("indicators-" + IoUtils.getCurrentDate() + ".xls")){
IoUtils.createIndicFile("indicators-" + IoUtils.getCurrentDate() + ".xls");
}
else IoUtils.INDIC_FILEPATH = "indicators-" + IoUtils.getCurrentDate() + ".xls";
Here is the function which give me the current date in a specific format:
// IoUtils class
public static String getCurrentDate(){
LocalDateTime ldt = LocalDateTime.now();
return DateTimeFormatter.ofPattern("dd-MM-yyyy", Locale.ENGLISH).format(ldt);
}
So how can I change this to only create a file per week ?
I would also like to have the month and the number of the week in the filename like this:
// first monday of january 2018
name = "indicators-week1-01-2018"
// second monday of january 2018
name = "indicators-week2-01-2018"
Thank you
java
1
by writing it that way ... just check whether the system day is a monday before writing the file
– Stultuske
Nov 28 '18 at 9:37
1
What is your problem ? To check if the current day is the Monday ? Or to schedule the creation ?
– vincrichaud
Nov 28 '18 at 9:39
@Stultuske please check the update
– Amadou Beye
Nov 28 '18 at 9:43
@vincrichaud I have updated the question
– Amadou Beye
Nov 28 '18 at 9:43
2
@AmadouBeye No problem from the bad English ;-). The problem is that you show tyhe code that create the file. You show the code the get the date. But where is the code that make it happen every day ?
– vincrichaud
Nov 28 '18 at 10:36
|
show 2 more comments
I have a java program which is gonna be manually run every day.
Now I want to create an excel file per wek to write in it all tasks of the week
I know how to create a file every day like this:
if(!IoUtils.fileExist("indicators-" + IoUtils.getCurrentDate() + ".xls")){
IoUtils.createIndicFile("indicators-" + IoUtils.getCurrentDate() + ".xls");
}
else IoUtils.INDIC_FILEPATH = "indicators-" + IoUtils.getCurrentDate() + ".xls";
Here is the function which give me the current date in a specific format:
// IoUtils class
public static String getCurrentDate(){
LocalDateTime ldt = LocalDateTime.now();
return DateTimeFormatter.ofPattern("dd-MM-yyyy", Locale.ENGLISH).format(ldt);
}
So how can I change this to only create a file per week ?
I would also like to have the month and the number of the week in the filename like this:
// first monday of january 2018
name = "indicators-week1-01-2018"
// second monday of january 2018
name = "indicators-week2-01-2018"
Thank you
java
I have a java program which is gonna be manually run every day.
Now I want to create an excel file per wek to write in it all tasks of the week
I know how to create a file every day like this:
if(!IoUtils.fileExist("indicators-" + IoUtils.getCurrentDate() + ".xls")){
IoUtils.createIndicFile("indicators-" + IoUtils.getCurrentDate() + ".xls");
}
else IoUtils.INDIC_FILEPATH = "indicators-" + IoUtils.getCurrentDate() + ".xls";
Here is the function which give me the current date in a specific format:
// IoUtils class
public static String getCurrentDate(){
LocalDateTime ldt = LocalDateTime.now();
return DateTimeFormatter.ofPattern("dd-MM-yyyy", Locale.ENGLISH).format(ldt);
}
So how can I change this to only create a file per week ?
I would also like to have the month and the number of the week in the filename like this:
// first monday of january 2018
name = "indicators-week1-01-2018"
// second monday of january 2018
name = "indicators-week2-01-2018"
Thank you
java
java
edited Nov 28 '18 at 9:52
Amadou Beye
asked Nov 28 '18 at 9:36
Amadou BeyeAmadou Beye
963716
963716
1
by writing it that way ... just check whether the system day is a monday before writing the file
– Stultuske
Nov 28 '18 at 9:37
1
What is your problem ? To check if the current day is the Monday ? Or to schedule the creation ?
– vincrichaud
Nov 28 '18 at 9:39
@Stultuske please check the update
– Amadou Beye
Nov 28 '18 at 9:43
@vincrichaud I have updated the question
– Amadou Beye
Nov 28 '18 at 9:43
2
@AmadouBeye No problem from the bad English ;-). The problem is that you show tyhe code that create the file. You show the code the get the date. But where is the code that make it happen every day ?
– vincrichaud
Nov 28 '18 at 10:36
|
show 2 more comments
1
by writing it that way ... just check whether the system day is a monday before writing the file
– Stultuske
Nov 28 '18 at 9:37
1
What is your problem ? To check if the current day is the Monday ? Or to schedule the creation ?
– vincrichaud
Nov 28 '18 at 9:39
@Stultuske please check the update
– Amadou Beye
Nov 28 '18 at 9:43
@vincrichaud I have updated the question
– Amadou Beye
Nov 28 '18 at 9:43
2
@AmadouBeye No problem from the bad English ;-). The problem is that you show tyhe code that create the file. You show the code the get the date. But where is the code that make it happen every day ?
– vincrichaud
Nov 28 '18 at 10:36
1
1
by writing it that way ... just check whether the system day is a monday before writing the file
– Stultuske
Nov 28 '18 at 9:37
by writing it that way ... just check whether the system day is a monday before writing the file
– Stultuske
Nov 28 '18 at 9:37
1
1
What is your problem ? To check if the current day is the Monday ? Or to schedule the creation ?
– vincrichaud
Nov 28 '18 at 9:39
What is your problem ? To check if the current day is the Monday ? Or to schedule the creation ?
– vincrichaud
Nov 28 '18 at 9:39
@Stultuske please check the update
– Amadou Beye
Nov 28 '18 at 9:43
@Stultuske please check the update
– Amadou Beye
Nov 28 '18 at 9:43
@vincrichaud I have updated the question
– Amadou Beye
Nov 28 '18 at 9:43
@vincrichaud I have updated the question
– Amadou Beye
Nov 28 '18 at 9:43
2
2
@AmadouBeye No problem from the bad English ;-). The problem is that you show tyhe code that create the file. You show the code the get the date. But where is the code that make it happen every day ?
– vincrichaud
Nov 28 '18 at 10:36
@AmadouBeye No problem from the bad English ;-). The problem is that you show tyhe code that create the file. You show the code the get the date. But where is the code that make it happen every day ?
– vincrichaud
Nov 28 '18 at 10:36
|
show 2 more comments
7 Answers
7
active
oldest
votes
Java provides java.util.concurrent.ScheduledThreadPoolExecutor that can additionally schedule commands to run after a given delay, or to execute periodically.
Scheduler.scheduleWithFixedDelay(task, StartDelay, repeatInterval, TimeUnit.MINUTES);
Please note that if this will be running on multiple instances, a report will be generated for every instance separately.
– Sofo Gial
Nov 28 '18 at 9:57
@SofoGial In theRunnable
run by the scheduled executor service, it is the job of the programmer to check for the correct date and to check if the file has been written yet for that week. So the executor repeating is a feature, not a problem.
– Basil Bourque
Nov 28 '18 at 21:18
@BasilBourque Absolutely! I have just pointed this out, since I have seen people forgetting this quite often in the last 5 years :)
– Sofo Gial
Nov 29 '18 at 8:46
add a comment |
Executor
Old-school way used the Timer
class.
New-school way uses the Executors framework that handle the nitty-gritty details of scheduling tasks on background threads.
Set up an executor to run a Runnable
every few hours. That task checks the current moment. If the day-of-week of the current date is a Monday, and if your file is not yet written, write it. If not, then let the Runnable expire. The scheduled executor service will run again in a few hours, and repeat again and again.
The first step is to get the current date.
A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.
If no time zone is specified, the JVM implicitly applies its current default time zone. That default may change at any moment during runtime(!), so your results may vary. Better to specify your desired/expected time zone explicitly as an argument.
Specify a proper time zone name in the format of continent/region
, such as America/Montreal
, Africa/Casablanca
, or Pacific/Auckland
. Never use the 2-4 letter abbreviation such as EST
or IST
as they are not true time zones, not standardized, and not even unique(!).
ZoneId z = ZoneId.of( "America/Montreal" ) ;
LocalDate today = LocalDate.now( z );
from that get the current day of the week.
DayOfWeek dow = today.getDayOfWeek() ;
If today is a Monday, then see if file has yet been written. If not, write it.
if( dow.equals( DayOfWeek.MONDAY ) ) {
if( file not written ) { write file }
}
Put that all together into a named method.
private void writeFileOnMonday ( ) {
ZoneId z = ZoneId.of( "America/Montreal" );
LocalDate today = LocalDate.now( z );
DayOfWeek dow = today.getDayOfWeek();
if ( dow.equals( DayOfWeek.MONDAY ) ) {
if ( file not written ){ write file }
}
}
Harness that workload in a scheduled executor service. Specify how many hours to wait between runs. If we specify to run our task every 3 hours, then, logic dictates, our weekly file will be written sometime between midnight and 3 AM on each Monday.
One big catch with a scheduled executor service: The repeating task execution comes silently to a halt if on any run a Throwable
(Exception
or Error
) is thrown by your task, and reaches the executor. So always wrap your task in a try-catch. See this Question.
ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(); // Convenience method to produce an executor service.
ScheduledFuture scheduledFuture = // A handle to check the status of your task. May or may not be useful to you.
scheduledExecutorService
.scheduleWithFixedDelay( new Runnable() { // Implement the `Runnable` interface as your task.
@Override
public void run ( ) {
try {
writeFileOnMonday();
} catch (Exception e ) {
… handle unexected exception
}
}
} ,
0 , // Initial delay
3 , // Delay between runs.
TimeUnit.HOURS ); // Unit of time meant for the pair of delay numbers above.
Search Stack Overflow for more info, as all this has been covered many many times already.
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
.
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.
Where to obtain the java.time classes?
Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
Java SE 6 and Java SE 7
- Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
Android
- Later versions of Android bundle implementations of the java.time classes.
- For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….
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.
add a comment |
Create a quartz cron job scheduler 0 0 12 ? * MON
.
This will schedule job on every Monday at 12.00 PM.
add a comment |
Since you're using LocalDateTime
, you can do a check using getDayOfWeek
method.
A example such as
if(LocalDateTime.now().getDayOfWeek() == DayOfWeek.MONDAY){
// File Creation Logic ...
}
While this code works, theZonedDateTime
class is more appropriate thatLocalDateTime
.
– Basil Bourque
Nov 28 '18 at 20:04
Agreed. Just working with what the question started with. Cheers.
– Samuel Kok
Nov 29 '18 at 1:58
add a comment |
use calender API, calendar.get(Calendar.DAY_OF_WEEK) will return int 2 for Monday.
import java.util.Calendar;
if(calendar.get(Calendar.DAY_OF_WEEK) == 2)
if(!IoUtils.fileExist("indicators-" + IoUtils.getCurrentDate() + ".xls")){
IoUtils.createIndicFile("indicators-" + IoUtils.getCurrentDate() + ".xls");
}
else IoUtils.INDIC_FILEPATH = "indicators-" + IoUtils.getCurrentDate() + ".xls";
FYI, the terribly troublesome old date-time classes such asjava.util.Date
,java.util.Calendar
, andjava.text.SimpleDateFormat
are now legacy, supplanted by the java.time classes built into Java 8 and later. See Tutorial by Oracle.
– Basil Bourque
Nov 28 '18 at 20:04
add a comment |
Calendar cal = Calendar.getInstance();
cal.setFirstDayOfWeek(Calendar.MONDAY);
int week = cal.get(Calendar.WEEK_OF_YEAR);
String expectedFileName = "indicators-week"+week+"-whateveryouwant-"+cal.get(Calendar.YEAR);
Then you can check and if the file exists , write to the same file, if file does not exist you will create a new file with the expect file name and write to that new file.but based on the requirement year break should be handled properly
FYI, the terribly troublesome old date-time classes such asjava.util.Date
,java.util.Calendar
, andjava.text.SimpleDateFormat
are now legacy, supplanted by the java.time classes built into Java 8 and later. See Tutorial by Oracle.
– Basil Bourque
Nov 28 '18 at 20:04
@Basil Bourque, thanks for the info
– hunter
Nov 29 '18 at 3:36
add a comment |
Use Calendar
:
Calendar cal = Calendar.getInstance();
int currentDay = cal.get(Calendar.DAY_OF_WEEK);
if (currentDay == Calendar.MONDAY) {
// write your code here
}
1
FYI, the terribly troublesome old date-time classes such asjava.util.Date
,java.util.Calendar
, andjava.text.SimpleDateFormat
are now legacy, supplanted by the java.time classes built into Java 8 and later. See Tutorial by Oracle.
– Basil Bourque
Nov 28 '18 at 20:03
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53516297%2fcreate-one-file-per-week%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
7 Answers
7
active
oldest
votes
7 Answers
7
active
oldest
votes
active
oldest
votes
active
oldest
votes
Java provides java.util.concurrent.ScheduledThreadPoolExecutor that can additionally schedule commands to run after a given delay, or to execute periodically.
Scheduler.scheduleWithFixedDelay(task, StartDelay, repeatInterval, TimeUnit.MINUTES);
Please note that if this will be running on multiple instances, a report will be generated for every instance separately.
– Sofo Gial
Nov 28 '18 at 9:57
@SofoGial In theRunnable
run by the scheduled executor service, it is the job of the programmer to check for the correct date and to check if the file has been written yet for that week. So the executor repeating is a feature, not a problem.
– Basil Bourque
Nov 28 '18 at 21:18
@BasilBourque Absolutely! I have just pointed this out, since I have seen people forgetting this quite often in the last 5 years :)
– Sofo Gial
Nov 29 '18 at 8:46
add a comment |
Java provides java.util.concurrent.ScheduledThreadPoolExecutor that can additionally schedule commands to run after a given delay, or to execute periodically.
Scheduler.scheduleWithFixedDelay(task, StartDelay, repeatInterval, TimeUnit.MINUTES);
Please note that if this will be running on multiple instances, a report will be generated for every instance separately.
– Sofo Gial
Nov 28 '18 at 9:57
@SofoGial In theRunnable
run by the scheduled executor service, it is the job of the programmer to check for the correct date and to check if the file has been written yet for that week. So the executor repeating is a feature, not a problem.
– Basil Bourque
Nov 28 '18 at 21:18
@BasilBourque Absolutely! I have just pointed this out, since I have seen people forgetting this quite often in the last 5 years :)
– Sofo Gial
Nov 29 '18 at 8:46
add a comment |
Java provides java.util.concurrent.ScheduledThreadPoolExecutor that can additionally schedule commands to run after a given delay, or to execute periodically.
Scheduler.scheduleWithFixedDelay(task, StartDelay, repeatInterval, TimeUnit.MINUTES);
Java provides java.util.concurrent.ScheduledThreadPoolExecutor that can additionally schedule commands to run after a given delay, or to execute periodically.
Scheduler.scheduleWithFixedDelay(task, StartDelay, repeatInterval, TimeUnit.MINUTES);
answered Nov 28 '18 at 9:52
VebbieVebbie
1,1301617
1,1301617
Please note that if this will be running on multiple instances, a report will be generated for every instance separately.
– Sofo Gial
Nov 28 '18 at 9:57
@SofoGial In theRunnable
run by the scheduled executor service, it is the job of the programmer to check for the correct date and to check if the file has been written yet for that week. So the executor repeating is a feature, not a problem.
– Basil Bourque
Nov 28 '18 at 21:18
@BasilBourque Absolutely! I have just pointed this out, since I have seen people forgetting this quite often in the last 5 years :)
– Sofo Gial
Nov 29 '18 at 8:46
add a comment |
Please note that if this will be running on multiple instances, a report will be generated for every instance separately.
– Sofo Gial
Nov 28 '18 at 9:57
@SofoGial In theRunnable
run by the scheduled executor service, it is the job of the programmer to check for the correct date and to check if the file has been written yet for that week. So the executor repeating is a feature, not a problem.
– Basil Bourque
Nov 28 '18 at 21:18
@BasilBourque Absolutely! I have just pointed this out, since I have seen people forgetting this quite often in the last 5 years :)
– Sofo Gial
Nov 29 '18 at 8:46
Please note that if this will be running on multiple instances, a report will be generated for every instance separately.
– Sofo Gial
Nov 28 '18 at 9:57
Please note that if this will be running on multiple instances, a report will be generated for every instance separately.
– Sofo Gial
Nov 28 '18 at 9:57
@SofoGial In the
Runnable
run by the scheduled executor service, it is the job of the programmer to check for the correct date and to check if the file has been written yet for that week. So the executor repeating is a feature, not a problem.– Basil Bourque
Nov 28 '18 at 21:18
@SofoGial In the
Runnable
run by the scheduled executor service, it is the job of the programmer to check for the correct date and to check if the file has been written yet for that week. So the executor repeating is a feature, not a problem.– Basil Bourque
Nov 28 '18 at 21:18
@BasilBourque Absolutely! I have just pointed this out, since I have seen people forgetting this quite often in the last 5 years :)
– Sofo Gial
Nov 29 '18 at 8:46
@BasilBourque Absolutely! I have just pointed this out, since I have seen people forgetting this quite often in the last 5 years :)
– Sofo Gial
Nov 29 '18 at 8:46
add a comment |
Executor
Old-school way used the Timer
class.
New-school way uses the Executors framework that handle the nitty-gritty details of scheduling tasks on background threads.
Set up an executor to run a Runnable
every few hours. That task checks the current moment. If the day-of-week of the current date is a Monday, and if your file is not yet written, write it. If not, then let the Runnable expire. The scheduled executor service will run again in a few hours, and repeat again and again.
The first step is to get the current date.
A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.
If no time zone is specified, the JVM implicitly applies its current default time zone. That default may change at any moment during runtime(!), so your results may vary. Better to specify your desired/expected time zone explicitly as an argument.
Specify a proper time zone name in the format of continent/region
, such as America/Montreal
, Africa/Casablanca
, or Pacific/Auckland
. Never use the 2-4 letter abbreviation such as EST
or IST
as they are not true time zones, not standardized, and not even unique(!).
ZoneId z = ZoneId.of( "America/Montreal" ) ;
LocalDate today = LocalDate.now( z );
from that get the current day of the week.
DayOfWeek dow = today.getDayOfWeek() ;
If today is a Monday, then see if file has yet been written. If not, write it.
if( dow.equals( DayOfWeek.MONDAY ) ) {
if( file not written ) { write file }
}
Put that all together into a named method.
private void writeFileOnMonday ( ) {
ZoneId z = ZoneId.of( "America/Montreal" );
LocalDate today = LocalDate.now( z );
DayOfWeek dow = today.getDayOfWeek();
if ( dow.equals( DayOfWeek.MONDAY ) ) {
if ( file not written ){ write file }
}
}
Harness that workload in a scheduled executor service. Specify how many hours to wait between runs. If we specify to run our task every 3 hours, then, logic dictates, our weekly file will be written sometime between midnight and 3 AM on each Monday.
One big catch with a scheduled executor service: The repeating task execution comes silently to a halt if on any run a Throwable
(Exception
or Error
) is thrown by your task, and reaches the executor. So always wrap your task in a try-catch. See this Question.
ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(); // Convenience method to produce an executor service.
ScheduledFuture scheduledFuture = // A handle to check the status of your task. May or may not be useful to you.
scheduledExecutorService
.scheduleWithFixedDelay( new Runnable() { // Implement the `Runnable` interface as your task.
@Override
public void run ( ) {
try {
writeFileOnMonday();
} catch (Exception e ) {
… handle unexected exception
}
}
} ,
0 , // Initial delay
3 , // Delay between runs.
TimeUnit.HOURS ); // Unit of time meant for the pair of delay numbers above.
Search Stack Overflow for more info, as all this has been covered many many times already.
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
.
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.
Where to obtain the java.time classes?
Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
Java SE 6 and Java SE 7
- Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
Android
- Later versions of Android bundle implementations of the java.time classes.
- For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….
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.
add a comment |
Executor
Old-school way used the Timer
class.
New-school way uses the Executors framework that handle the nitty-gritty details of scheduling tasks on background threads.
Set up an executor to run a Runnable
every few hours. That task checks the current moment. If the day-of-week of the current date is a Monday, and if your file is not yet written, write it. If not, then let the Runnable expire. The scheduled executor service will run again in a few hours, and repeat again and again.
The first step is to get the current date.
A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.
If no time zone is specified, the JVM implicitly applies its current default time zone. That default may change at any moment during runtime(!), so your results may vary. Better to specify your desired/expected time zone explicitly as an argument.
Specify a proper time zone name in the format of continent/region
, such as America/Montreal
, Africa/Casablanca
, or Pacific/Auckland
. Never use the 2-4 letter abbreviation such as EST
or IST
as they are not true time zones, not standardized, and not even unique(!).
ZoneId z = ZoneId.of( "America/Montreal" ) ;
LocalDate today = LocalDate.now( z );
from that get the current day of the week.
DayOfWeek dow = today.getDayOfWeek() ;
If today is a Monday, then see if file has yet been written. If not, write it.
if( dow.equals( DayOfWeek.MONDAY ) ) {
if( file not written ) { write file }
}
Put that all together into a named method.
private void writeFileOnMonday ( ) {
ZoneId z = ZoneId.of( "America/Montreal" );
LocalDate today = LocalDate.now( z );
DayOfWeek dow = today.getDayOfWeek();
if ( dow.equals( DayOfWeek.MONDAY ) ) {
if ( file not written ){ write file }
}
}
Harness that workload in a scheduled executor service. Specify how many hours to wait between runs. If we specify to run our task every 3 hours, then, logic dictates, our weekly file will be written sometime between midnight and 3 AM on each Monday.
One big catch with a scheduled executor service: The repeating task execution comes silently to a halt if on any run a Throwable
(Exception
or Error
) is thrown by your task, and reaches the executor. So always wrap your task in a try-catch. See this Question.
ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(); // Convenience method to produce an executor service.
ScheduledFuture scheduledFuture = // A handle to check the status of your task. May or may not be useful to you.
scheduledExecutorService
.scheduleWithFixedDelay( new Runnable() { // Implement the `Runnable` interface as your task.
@Override
public void run ( ) {
try {
writeFileOnMonday();
} catch (Exception e ) {
… handle unexected exception
}
}
} ,
0 , // Initial delay
3 , // Delay between runs.
TimeUnit.HOURS ); // Unit of time meant for the pair of delay numbers above.
Search Stack Overflow for more info, as all this has been covered many many times already.
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
.
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.
Where to obtain the java.time classes?
Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
Java SE 6 and Java SE 7
- Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
Android
- Later versions of Android bundle implementations of the java.time classes.
- For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….
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.
add a comment |
Executor
Old-school way used the Timer
class.
New-school way uses the Executors framework that handle the nitty-gritty details of scheduling tasks on background threads.
Set up an executor to run a Runnable
every few hours. That task checks the current moment. If the day-of-week of the current date is a Monday, and if your file is not yet written, write it. If not, then let the Runnable expire. The scheduled executor service will run again in a few hours, and repeat again and again.
The first step is to get the current date.
A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.
If no time zone is specified, the JVM implicitly applies its current default time zone. That default may change at any moment during runtime(!), so your results may vary. Better to specify your desired/expected time zone explicitly as an argument.
Specify a proper time zone name in the format of continent/region
, such as America/Montreal
, Africa/Casablanca
, or Pacific/Auckland
. Never use the 2-4 letter abbreviation such as EST
or IST
as they are not true time zones, not standardized, and not even unique(!).
ZoneId z = ZoneId.of( "America/Montreal" ) ;
LocalDate today = LocalDate.now( z );
from that get the current day of the week.
DayOfWeek dow = today.getDayOfWeek() ;
If today is a Monday, then see if file has yet been written. If not, write it.
if( dow.equals( DayOfWeek.MONDAY ) ) {
if( file not written ) { write file }
}
Put that all together into a named method.
private void writeFileOnMonday ( ) {
ZoneId z = ZoneId.of( "America/Montreal" );
LocalDate today = LocalDate.now( z );
DayOfWeek dow = today.getDayOfWeek();
if ( dow.equals( DayOfWeek.MONDAY ) ) {
if ( file not written ){ write file }
}
}
Harness that workload in a scheduled executor service. Specify how many hours to wait between runs. If we specify to run our task every 3 hours, then, logic dictates, our weekly file will be written sometime between midnight and 3 AM on each Monday.
One big catch with a scheduled executor service: The repeating task execution comes silently to a halt if on any run a Throwable
(Exception
or Error
) is thrown by your task, and reaches the executor. So always wrap your task in a try-catch. See this Question.
ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(); // Convenience method to produce an executor service.
ScheduledFuture scheduledFuture = // A handle to check the status of your task. May or may not be useful to you.
scheduledExecutorService
.scheduleWithFixedDelay( new Runnable() { // Implement the `Runnable` interface as your task.
@Override
public void run ( ) {
try {
writeFileOnMonday();
} catch (Exception e ) {
… handle unexected exception
}
}
} ,
0 , // Initial delay
3 , // Delay between runs.
TimeUnit.HOURS ); // Unit of time meant for the pair of delay numbers above.
Search Stack Overflow for more info, as all this has been covered many many times already.
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
.
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.
Where to obtain the java.time classes?
Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
Java SE 6 and Java SE 7
- Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
Android
- Later versions of Android bundle implementations of the java.time classes.
- For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….
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.
Executor
Old-school way used the Timer
class.
New-school way uses the Executors framework that handle the nitty-gritty details of scheduling tasks on background threads.
Set up an executor to run a Runnable
every few hours. That task checks the current moment. If the day-of-week of the current date is a Monday, and if your file is not yet written, write it. If not, then let the Runnable expire. The scheduled executor service will run again in a few hours, and repeat again and again.
The first step is to get the current date.
A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.
If no time zone is specified, the JVM implicitly applies its current default time zone. That default may change at any moment during runtime(!), so your results may vary. Better to specify your desired/expected time zone explicitly as an argument.
Specify a proper time zone name in the format of continent/region
, such as America/Montreal
, Africa/Casablanca
, or Pacific/Auckland
. Never use the 2-4 letter abbreviation such as EST
or IST
as they are not true time zones, not standardized, and not even unique(!).
ZoneId z = ZoneId.of( "America/Montreal" ) ;
LocalDate today = LocalDate.now( z );
from that get the current day of the week.
DayOfWeek dow = today.getDayOfWeek() ;
If today is a Monday, then see if file has yet been written. If not, write it.
if( dow.equals( DayOfWeek.MONDAY ) ) {
if( file not written ) { write file }
}
Put that all together into a named method.
private void writeFileOnMonday ( ) {
ZoneId z = ZoneId.of( "America/Montreal" );
LocalDate today = LocalDate.now( z );
DayOfWeek dow = today.getDayOfWeek();
if ( dow.equals( DayOfWeek.MONDAY ) ) {
if ( file not written ){ write file }
}
}
Harness that workload in a scheduled executor service. Specify how many hours to wait between runs. If we specify to run our task every 3 hours, then, logic dictates, our weekly file will be written sometime between midnight and 3 AM on each Monday.
One big catch with a scheduled executor service: The repeating task execution comes silently to a halt if on any run a Throwable
(Exception
or Error
) is thrown by your task, and reaches the executor. So always wrap your task in a try-catch. See this Question.
ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(); // Convenience method to produce an executor service.
ScheduledFuture scheduledFuture = // A handle to check the status of your task. May or may not be useful to you.
scheduledExecutorService
.scheduleWithFixedDelay( new Runnable() { // Implement the `Runnable` interface as your task.
@Override
public void run ( ) {
try {
writeFileOnMonday();
} catch (Exception e ) {
… handle unexected exception
}
}
} ,
0 , // Initial delay
3 , // Delay between runs.
TimeUnit.HOURS ); // Unit of time meant for the pair of delay numbers above.
Search Stack Overflow for more info, as all this has been covered many many times already.
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
.
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.
Where to obtain the java.time classes?
Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
Java SE 6 and Java SE 7
- Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
Android
- Later versions of Android bundle implementations of the java.time classes.
- For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….
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.
edited Nov 29 '18 at 16:19
answered Nov 28 '18 at 20:15
Basil BourqueBasil Bourque
115k29393556
115k29393556
add a comment |
add a comment |
Create a quartz cron job scheduler 0 0 12 ? * MON
.
This will schedule job on every Monday at 12.00 PM.
add a comment |
Create a quartz cron job scheduler 0 0 12 ? * MON
.
This will schedule job on every Monday at 12.00 PM.
add a comment |
Create a quartz cron job scheduler 0 0 12 ? * MON
.
This will schedule job on every Monday at 12.00 PM.
Create a quartz cron job scheduler 0 0 12 ? * MON
.
This will schedule job on every Monday at 12.00 PM.
answered Nov 28 '18 at 9:48
salzsalz
419
419
add a comment |
add a comment |
Since you're using LocalDateTime
, you can do a check using getDayOfWeek
method.
A example such as
if(LocalDateTime.now().getDayOfWeek() == DayOfWeek.MONDAY){
// File Creation Logic ...
}
While this code works, theZonedDateTime
class is more appropriate thatLocalDateTime
.
– Basil Bourque
Nov 28 '18 at 20:04
Agreed. Just working with what the question started with. Cheers.
– Samuel Kok
Nov 29 '18 at 1:58
add a comment |
Since you're using LocalDateTime
, you can do a check using getDayOfWeek
method.
A example such as
if(LocalDateTime.now().getDayOfWeek() == DayOfWeek.MONDAY){
// File Creation Logic ...
}
While this code works, theZonedDateTime
class is more appropriate thatLocalDateTime
.
– Basil Bourque
Nov 28 '18 at 20:04
Agreed. Just working with what the question started with. Cheers.
– Samuel Kok
Nov 29 '18 at 1:58
add a comment |
Since you're using LocalDateTime
, you can do a check using getDayOfWeek
method.
A example such as
if(LocalDateTime.now().getDayOfWeek() == DayOfWeek.MONDAY){
// File Creation Logic ...
}
Since you're using LocalDateTime
, you can do a check using getDayOfWeek
method.
A example such as
if(LocalDateTime.now().getDayOfWeek() == DayOfWeek.MONDAY){
// File Creation Logic ...
}
answered Nov 28 '18 at 9:50
Samuel KokSamuel Kok
549816
549816
While this code works, theZonedDateTime
class is more appropriate thatLocalDateTime
.
– Basil Bourque
Nov 28 '18 at 20:04
Agreed. Just working with what the question started with. Cheers.
– Samuel Kok
Nov 29 '18 at 1:58
add a comment |
While this code works, theZonedDateTime
class is more appropriate thatLocalDateTime
.
– Basil Bourque
Nov 28 '18 at 20:04
Agreed. Just working with what the question started with. Cheers.
– Samuel Kok
Nov 29 '18 at 1:58
While this code works, the
ZonedDateTime
class is more appropriate that LocalDateTime
.– Basil Bourque
Nov 28 '18 at 20:04
While this code works, the
ZonedDateTime
class is more appropriate that LocalDateTime
.– Basil Bourque
Nov 28 '18 at 20:04
Agreed. Just working with what the question started with. Cheers.
– Samuel Kok
Nov 29 '18 at 1:58
Agreed. Just working with what the question started with. Cheers.
– Samuel Kok
Nov 29 '18 at 1:58
add a comment |
use calender API, calendar.get(Calendar.DAY_OF_WEEK) will return int 2 for Monday.
import java.util.Calendar;
if(calendar.get(Calendar.DAY_OF_WEEK) == 2)
if(!IoUtils.fileExist("indicators-" + IoUtils.getCurrentDate() + ".xls")){
IoUtils.createIndicFile("indicators-" + IoUtils.getCurrentDate() + ".xls");
}
else IoUtils.INDIC_FILEPATH = "indicators-" + IoUtils.getCurrentDate() + ".xls";
FYI, the terribly troublesome old date-time classes such asjava.util.Date
,java.util.Calendar
, andjava.text.SimpleDateFormat
are now legacy, supplanted by the java.time classes built into Java 8 and later. See Tutorial by Oracle.
– Basil Bourque
Nov 28 '18 at 20:04
add a comment |
use calender API, calendar.get(Calendar.DAY_OF_WEEK) will return int 2 for Monday.
import java.util.Calendar;
if(calendar.get(Calendar.DAY_OF_WEEK) == 2)
if(!IoUtils.fileExist("indicators-" + IoUtils.getCurrentDate() + ".xls")){
IoUtils.createIndicFile("indicators-" + IoUtils.getCurrentDate() + ".xls");
}
else IoUtils.INDIC_FILEPATH = "indicators-" + IoUtils.getCurrentDate() + ".xls";
FYI, the terribly troublesome old date-time classes such asjava.util.Date
,java.util.Calendar
, andjava.text.SimpleDateFormat
are now legacy, supplanted by the java.time classes built into Java 8 and later. See Tutorial by Oracle.
– Basil Bourque
Nov 28 '18 at 20:04
add a comment |
use calender API, calendar.get(Calendar.DAY_OF_WEEK) will return int 2 for Monday.
import java.util.Calendar;
if(calendar.get(Calendar.DAY_OF_WEEK) == 2)
if(!IoUtils.fileExist("indicators-" + IoUtils.getCurrentDate() + ".xls")){
IoUtils.createIndicFile("indicators-" + IoUtils.getCurrentDate() + ".xls");
}
else IoUtils.INDIC_FILEPATH = "indicators-" + IoUtils.getCurrentDate() + ".xls";
use calender API, calendar.get(Calendar.DAY_OF_WEEK) will return int 2 for Monday.
import java.util.Calendar;
if(calendar.get(Calendar.DAY_OF_WEEK) == 2)
if(!IoUtils.fileExist("indicators-" + IoUtils.getCurrentDate() + ".xls")){
IoUtils.createIndicFile("indicators-" + IoUtils.getCurrentDate() + ".xls");
}
else IoUtils.INDIC_FILEPATH = "indicators-" + IoUtils.getCurrentDate() + ".xls";
answered Nov 28 '18 at 9:57
himanshu agarwalhimanshu agarwal
1
1
FYI, the terribly troublesome old date-time classes such asjava.util.Date
,java.util.Calendar
, andjava.text.SimpleDateFormat
are now legacy, supplanted by the java.time classes built into Java 8 and later. See Tutorial by Oracle.
– Basil Bourque
Nov 28 '18 at 20:04
add a comment |
FYI, the terribly troublesome old date-time classes such asjava.util.Date
,java.util.Calendar
, andjava.text.SimpleDateFormat
are now legacy, supplanted by the java.time classes built into Java 8 and later. See Tutorial by Oracle.
– Basil Bourque
Nov 28 '18 at 20:04
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
Nov 28 '18 at 20:04
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
Nov 28 '18 at 20:04
add a comment |
Calendar cal = Calendar.getInstance();
cal.setFirstDayOfWeek(Calendar.MONDAY);
int week = cal.get(Calendar.WEEK_OF_YEAR);
String expectedFileName = "indicators-week"+week+"-whateveryouwant-"+cal.get(Calendar.YEAR);
Then you can check and if the file exists , write to the same file, if file does not exist you will create a new file with the expect file name and write to that new file.but based on the requirement year break should be handled properly
FYI, the terribly troublesome old date-time classes such asjava.util.Date
,java.util.Calendar
, andjava.text.SimpleDateFormat
are now legacy, supplanted by the java.time classes built into Java 8 and later. See Tutorial by Oracle.
– Basil Bourque
Nov 28 '18 at 20:04
@Basil Bourque, thanks for the info
– hunter
Nov 29 '18 at 3:36
add a comment |
Calendar cal = Calendar.getInstance();
cal.setFirstDayOfWeek(Calendar.MONDAY);
int week = cal.get(Calendar.WEEK_OF_YEAR);
String expectedFileName = "indicators-week"+week+"-whateveryouwant-"+cal.get(Calendar.YEAR);
Then you can check and if the file exists , write to the same file, if file does not exist you will create a new file with the expect file name and write to that new file.but based on the requirement year break should be handled properly
FYI, the terribly troublesome old date-time classes such asjava.util.Date
,java.util.Calendar
, andjava.text.SimpleDateFormat
are now legacy, supplanted by the java.time classes built into Java 8 and later. See Tutorial by Oracle.
– Basil Bourque
Nov 28 '18 at 20:04
@Basil Bourque, thanks for the info
– hunter
Nov 29 '18 at 3:36
add a comment |
Calendar cal = Calendar.getInstance();
cal.setFirstDayOfWeek(Calendar.MONDAY);
int week = cal.get(Calendar.WEEK_OF_YEAR);
String expectedFileName = "indicators-week"+week+"-whateveryouwant-"+cal.get(Calendar.YEAR);
Then you can check and if the file exists , write to the same file, if file does not exist you will create a new file with the expect file name and write to that new file.but based on the requirement year break should be handled properly
Calendar cal = Calendar.getInstance();
cal.setFirstDayOfWeek(Calendar.MONDAY);
int week = cal.get(Calendar.WEEK_OF_YEAR);
String expectedFileName = "indicators-week"+week+"-whateveryouwant-"+cal.get(Calendar.YEAR);
Then you can check and if the file exists , write to the same file, if file does not exist you will create a new file with the expect file name and write to that new file.but based on the requirement year break should be handled properly
answered Nov 28 '18 at 10:01
hunterhunter
1,614715
1,614715
FYI, the terribly troublesome old date-time classes such asjava.util.Date
,java.util.Calendar
, andjava.text.SimpleDateFormat
are now legacy, supplanted by the java.time classes built into Java 8 and later. See Tutorial by Oracle.
– Basil Bourque
Nov 28 '18 at 20:04
@Basil Bourque, thanks for the info
– hunter
Nov 29 '18 at 3:36
add a comment |
FYI, the terribly troublesome old date-time classes such asjava.util.Date
,java.util.Calendar
, andjava.text.SimpleDateFormat
are now legacy, supplanted by the java.time classes built into Java 8 and later. See Tutorial by Oracle.
– Basil Bourque
Nov 28 '18 at 20:04
@Basil Bourque, thanks for the info
– hunter
Nov 29 '18 at 3:36
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
Nov 28 '18 at 20:04
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
Nov 28 '18 at 20:04
@Basil Bourque, thanks for the info
– hunter
Nov 29 '18 at 3:36
@Basil Bourque, thanks for the info
– hunter
Nov 29 '18 at 3:36
add a comment |
Use Calendar
:
Calendar cal = Calendar.getInstance();
int currentDay = cal.get(Calendar.DAY_OF_WEEK);
if (currentDay == Calendar.MONDAY) {
// write your code here
}
1
FYI, the terribly troublesome old date-time classes such asjava.util.Date
,java.util.Calendar
, andjava.text.SimpleDateFormat
are now legacy, supplanted by the java.time classes built into Java 8 and later. See Tutorial by Oracle.
– Basil Bourque
Nov 28 '18 at 20:03
add a comment |
Use Calendar
:
Calendar cal = Calendar.getInstance();
int currentDay = cal.get(Calendar.DAY_OF_WEEK);
if (currentDay == Calendar.MONDAY) {
// write your code here
}
1
FYI, the terribly troublesome old date-time classes such asjava.util.Date
,java.util.Calendar
, andjava.text.SimpleDateFormat
are now legacy, supplanted by the java.time classes built into Java 8 and later. See Tutorial by Oracle.
– Basil Bourque
Nov 28 '18 at 20:03
add a comment |
Use Calendar
:
Calendar cal = Calendar.getInstance();
int currentDay = cal.get(Calendar.DAY_OF_WEEK);
if (currentDay == Calendar.MONDAY) {
// write your code here
}
Use Calendar
:
Calendar cal = Calendar.getInstance();
int currentDay = cal.get(Calendar.DAY_OF_WEEK);
if (currentDay == Calendar.MONDAY) {
// write your code here
}
edited Nov 28 '18 at 10:08
answered Nov 28 '18 at 9:43
Saeed HassanvandSaeed Hassanvand
340417
340417
1
FYI, the terribly troublesome old date-time classes such asjava.util.Date
,java.util.Calendar
, andjava.text.SimpleDateFormat
are now legacy, supplanted by the java.time classes built into Java 8 and later. See Tutorial by Oracle.
– Basil Bourque
Nov 28 '18 at 20:03
add a comment |
1
FYI, the terribly troublesome old date-time classes such asjava.util.Date
,java.util.Calendar
, andjava.text.SimpleDateFormat
are now legacy, supplanted by the java.time classes built into Java 8 and later. See Tutorial by Oracle.
– Basil Bourque
Nov 28 '18 at 20:03
1
1
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
Nov 28 '18 at 20:03
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
Nov 28 '18 at 20:03
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53516297%2fcreate-one-file-per-week%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
by writing it that way ... just check whether the system day is a monday before writing the file
– Stultuske
Nov 28 '18 at 9:37
1
What is your problem ? To check if the current day is the Monday ? Or to schedule the creation ?
– vincrichaud
Nov 28 '18 at 9:39
@Stultuske please check the update
– Amadou Beye
Nov 28 '18 at 9:43
@vincrichaud I have updated the question
– Amadou Beye
Nov 28 '18 at 9:43
2
@AmadouBeye No problem from the bad English ;-). The problem is that you show tyhe code that create the file. You show the code the get the date. But where is the code that make it happen every day ?
– vincrichaud
Nov 28 '18 at 10:36