This question already has answers here:
How to determine day of week by passing specific date?
(28 answers)
Closed 8 years ago.
I created a program that is running daily at a certain time,
but I want this program to stop at some particular days(week ends),
I have used the below codes to set the current time,
public int GetDateNow()
{
Calendar currentdate = Calendar.getInstance();
DateFormat dateformat = new SimpleDateFormat("HHmm");
String datenow = dateformat.format(currentdate.getTime());
int DN=Integer.parseInt(datenow);
return DN;
}
and the below code in the main class
while(true)
{
Thread.sleep(1*1000);
if(gt.GetDateNow()==0000)
{
//perform action
}
}
Use Calendar
calendarInstance.get(Calendar.DAY_OF_WEEK);
and compare it against weekends (Calendar.SATURDAY, Calendar.SUNDAY or depending on country)
Spring Framework provides scheduling of tasks based on cron expressions, all you need is to configure your tasks in context.xml e.g.
<task:scheduled ref="task" method="execute" cron="* 15 9-17 * * MON-FRI" />
OK, thanks guys it is working fine after I used the codes
calendarInstance.get(Calendar.DAY_OF_WEEK);
all bests,
Related
This question already has answers here:
want current date and time in "dd/MM/yyyy HH:mm:ss.SS" format
(11 answers)
How to convert date in to yyyy-MM-dd Format?
(6 answers)
Closed 2 years ago.
I have a
String s = "2020-02-22"`;
and I want to change it to Date so I can store it in my database which has has a column that does not accept anything but Date.
I tried using the LocalDate class but it's in API 26.
Any help would be appreciated.
Assume that you fetch the date from database and pass it to the below method:
public String formatDate(Date date){
SimpleDateFormat ff = new SimpleDateFormat("yyyy-MM-dd");
return ff.format(date);
}
EDIT : based on input from Basil, you could try Android Desugaring to make use of Java 8+ functionality without the need of minimum API level. This would allow the use of LocalDate instead of the old java.util.Date class.
Using LocalDate you could parse a string to date as:
public LocalDate getDate(String dateString) {
return LocalDate.parse(dateString);
}
This question already has answers here:
java DateTimeFormatterBuilder fails on testtime [duplicate]
(2 answers)
Closed 4 years ago.
I have this code fragment which worked previously for half year ( I wrote it myself ).
Yesterday I received new laptop with windows 10 ( previously 8.1 ) installed the most recent Java JDK jdk1.8.0_181 and this code stopped working with error.
Is it something I was missing for the whole time or there were some changes in java internal API ?
How I can fix it ? I believe it was written properly.
Caused by: java.time.format.DateTimeParseException: Text '29-Apr-2010,13:00:14' could not be parsed at index 3
private static final DateTimeFormatter PP_FORMATTER = DateTimeFormatter.ofPattern("d-MMM-yyyy,HH:mm:ss");
private static final LocalDate DATE = LocalDate.parse("29-Apr-2010,13:00:14", PP_FORMATTER);
try to add Locale.US
private static final DateTimeFormatter PP_FORMATTER = DateTimeFormatter.ofPattern("d-MMM-yyyy,HH:mm:ss", Locale.US);
otherwise, you maybe able to parse only numeric format for the month.
So I got this Instant date in Java: "2018-05-19T22:00:00Z".
How can I get the day of the week? like monday..
I was trying this but it doesn't work:
${date?string["EEEE"]}
Thanks
Freemarker does not work with Instant, so you would need to convert that to string in Java (something like "2018-05-19T22:00:00Z") and then convert the string doing the following:
<#setting locale="en_US">
${"2018-05-19T22:00:00Z"?datetime.iso?string["EEEE"]}
First convert the string to datetime in iso format, and then back again to string with the pattern of your choosing.
By changing the locale setting, you may get the day of the week in different languages.
I think that the best way to work in Freemarker is to always have strings or integers variables.
At this point FreeMarker does not support Java 8 time, see the contribute page:
What should I contribute?
Usually, contributors come because they want to fix/improve a certain thing. But if you just want to help in general, here are some topics ...
Support for Java 8 date/time API-s (this is actually certainly a difficult one). Note that there's a 3rd party solution for this, https://github.com/amedia/freemarker-java-8, but it's not as seamless as a native solution could be.
...
So you could check out the third party solution.
See also:
Java.time (Java 8) support in Freemarker
${date?string["EEEE"]} works fine as long as date is a java.util.Date object.
You can test it in this way:
<#assign date = .now>
${date?string["EEEE"]}
I guess that it doesn't work because your date is a String, in this case you should parse it in Java (server-side) and expose a java.util.Date variable to the template.
I would advise you to use new Date Time API defined in Java 8.Using Date Time API you can fetch all data related to Date and Time easily.
Java 8 has defined a separate Enum for handling days of the week named – DayOfWeek (java.time.DayOfWeek)
java.time.DayOfWeek is an Enum which defines 7 constants representing the seven days of the week – MONDAY(int value=1), TUESDAY(2), WEDNESDAY(3)… till SUNDAY(7).
import java.time.*;
public class Java8Tester {
public static void main(String args[]) {
Java8Tester java8tester = new Java8Tester();
java8tester.testLocalDateTime();
}
public void testLocalDateTime() {
// Get the current date and time
LocalDateTime currentTime = LocalDateTime.parse("2018-05-19T22:00:00");
System.out.println("Current DateTime: " + currentTime);
DayOfWeek dayOfWeek = currentTime.getDayOfWeek();
System.out.println(currentTime + " was a " + dayOfWeek);
}
}
This question already has answers here:
Code fragments are being struck out in Eclipse, why?
(3 answers)
Closed 7 years ago.
I have a piece of code here to calculate someone's age with the SQL date type in java.
The code works fine, but however you can't see it in the code pasted here, but in my netbeans environment the get methods are crossed out, but only in this line:
LocalDate datumDB = LocalDate.of(gbdat.getYear() + 1900, gbdat.getMonth() + 1,
gbdat.getDate());
someone any idea why?
this line isn't crossed out:
int leeftijd = datumVanVandaag.getYear() - datumDB.getYear();
this is my code:
private void checkSpelerGeschiktVoorPloeg(Persoon p) {
Date gbdat = p.getGbDatum();
LocalDate datumVanVandaag = LocalDate.now();
LocalDate datumDB = LocalDate.of(gbdat.getYear() + 1900, gbdat.getMonth() + 1, gbdat.getDate());
int leeftijd = datumVanVandaag.getYear() - datumDB.getYear();
}
The strikeout indicates that those methods are deprecated. You should use either java.util.Calendar or the new Java 8 Datetime API (which is the new-and-improved API that the LocalDate you're using is part of) to calculate these values for a date.
For instance the API document says:
#Deprecated
public int getYear()
Deprecated. As of JDK version 1.1, replaced by Calendar.get(Calendar.YEAR) - 1900.
Returns a value that is the result of subtracting 1900 from the year that contains or begins with the instant in time represented by this Date object, as interpreted in the local time zone.
This question already has answers here:
Java: Date from unix timestamp
(11 answers)
Closed 8 years ago.
In my android app I'm creating a timestamp this way:
final BackupInfo backupInfo = new BackupInfo(description, System.currentTimeMillis(), backupContacts.size());
eg, using System.currentTimeMillis()
Now I convert it back to date format using:
public static String getDate(long time)
{
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(time);
String date = DateFormat.format("dd-MM-yyyy HH:mm:ss", cal).toString();
return date;
}
And it works fine.
But now I'm receiving a timestamp from a server and the date String I receive from getDate is not the correct date.
Practical case:
My app generates this timestamp: 1403022230766
getDate returns this date: 17-06-2014 05:23:50 which is correct to my eyes.
Now the problem comes in, I get this timestamp from the server: 1403022360
getdate returns this date: 16-01-1970 18:43:42 which is totally wrong, it should be close to the timestamp generated by my app.
The timestamp returned by the server is 3 digits less in size. But if I go to an online converter, like this one and I put 1403022360 (the TS generated by the server) I get a correct date.
Can anyone explain me why this difference and what am I doing wrong in my getDate method that I can't decode the timestamp received from the server?
Your server is returning your timestamp in seconds, so multiply by 1000 to get milliseconds.
The online converters work properly because they assume that the that if the number is large enough, then it is in milliseconds and if it's short then it is in seconds.
Java/Android dates are all long types so they can hold milliseconds for additional precision.