I am new to java can anyone explain to me how this code works in this example
specially the LOOP portion
import java.util.*;
import static java.lang.System.out;
class FullMoons {
static int DAY_IM = 1000 * 60 * 60 * 24;
public static void main(String [] args) {
Calendar c = Calendar.getInstance();
c.set(2004,0,7,15,40);
long day1 = c.getTimeInMillis();
for (int x = 0; x < 60; x++) {
day1 += (DAY_IM * 29.52);
c.setTimeInMillis(day1);
out.println(String.format("full moon on %tc", c));
}
}
}
output is
full moon on Fri Feb 06 04:06:35 MST 2004
full moon on Sat Mar 06 16:38:23 MST 2004
full moon on Mon Apr 05 06:07:11 MDT 2004
an answer should be displayed in first three loops why it takes entire 60 runnings
What is the answer and why it should be displayed in first 3 loops?
This code creates Calendar and then added to the first full moon date the duration of lunar month (about 29.52 days).
The cycle body runs 60 times, because condition x < 60 and x++ increase x by 1.
//increase number of milliseconds
day1 += (DAY_IM * 29.52);
//set increased number to Calendar c
c.setTimeInMillis(day1);
//create formatted string
String s = String.format("full moon on %tc", c);
//output the string
out.println(s);
read more about how String.format() works in official documentation
This code will run 60 times throughout the Loop..
I realized my mistake which is iam only going by theory rather than doing practical...so guys this is lesson, and iam posting the result so that this question dosent create any unnecessary confusion further.
full moon on Fri Feb 06 04:08:48 IST 2004
full moon on Sat Mar 06 16:37:36 IST 2004
full moon on Mon Apr 05 05:06:24 IST 2004
full moon on Tue May 04 17:35:12 IST 2004
full moon on Thu Jun 03 06:04:00 IST 2004
full moon on Fri Jul 02 18:32:48 IST 2004
full moon on Sun Aug 01 07:01:36 IST 2004
full moon on Mon Aug 30 19:30:24 IST 2004
full moon on Wed Sep 29 07:59:12 IST 2004
full moon on Thu Oct 28 20:28:00 IST 2004
full moon on Sat Nov 27 08:56:48 IST 2004
full moon on Sun Dec 26 21:25:36 IST 2004
full moon on Tue Jan 25 09:54:24 IST 2005
full moon on Wed Feb 23 22:23:12 IST 2005
full moon on Fri Mar 25 10:52:00 IST 2005
full moon on Sat Apr 23 23:20:48 IST 2005
full moon on Mon May 23 11:49:36 IST 2005
full moon on Wed Jun 22 00:18:24 IST 2005
full moon on Thu Jul 21 12:47:12 IST 2005
full moon on Sat Aug 20 01:16:00 IST 2005
full moon on Sun Sep 18 13:44:48 IST 2005
full moon on Tue Oct 18 02:13:36 IST 2005
full moon on Wed Nov 16 14:42:24 IST 2005
full moon on Fri Dec 16 03:11:12 IST 2005
full moon on Sat Jan 14 15:40:00 IST 2006
full moon on Mon Feb 13 04:08:48 IST 2006
full moon on Tue Mar 14 16:37:36 IST 2006
full moon on Thu Apr 13 05:06:24 IST 2006
full moon on Fri May 12 17:35:12 IST 2006
full moon on Sun Jun 11 06:04:00 IST 2006
full moon on Mon Jul 10 18:32:48 IST 2006
full moon on Wed Aug 09 07:01:36 IST 2006
full moon on Thu Sep 07 19:30:24 IST 2006
full moon on Sat Oct 07 07:59:12 IST 2006
full moon on Sun Nov 05 20:28:00 IST 2006
full moon on Tue Dec 05 08:56:48 IST 2006
full moon on Wed Jan 03 21:25:36 IST 2007
full moon on Fri Feb 02 09:54:24 IST 2007
full moon on Sat Mar 03 22:23:12 IST 2007
full moon on Mon Apr 02 10:52:00 IST 2007
full moon on Tue May 01 23:20:48 IST 2007
full moon on Thu May 31 11:49:36 IST 2007
full moon on Sat Jun 30 00:18:24 IST 2007
full moon on Sun Jul 29 12:47:12 IST 2007
full moon on Tue Aug 28 01:16:00 IST 2007
full moon on Wed Sep 26 13:44:48 IST 2007
full moon on Fri Oct 26 02:13:36 IST 2007
full moon on Sat Nov 24 14:42:24 IST 2007
full moon on Mon Dec 24 03:11:12 IST 2007
full moon on Tue Jan 22 15:40:00 IST 2008
full moon on Thu Feb 21 04:08:48 IST 2008
full moon on Fri Mar 21 16:37:36 IST 2008
full moon on Sun Apr 20 05:06:24 IST 2008
full moon on Mon May 19 17:35:12 IST 2008
full moon on Wed Jun 18 06:04:00 IST 2008
full moon on Thu Jul 17 18:32:48 IST 2008
full moon on Sat Aug 16 07:01:36 IST 2008
full moon on Sun Sep 14 19:30:24 IST 2008
full moon on Tue Oct 14 07:59:12 IST 2008
full moon on Wed Nov 12 20:28:00 IST 2008
Related
Here is the documentation for the Spring Crontab syntax.
I want to run a task every weekday at 14:40, 14:45, 14:50, 14:55, and 15:00, but I can't figure out how to express this with a Crontab pattern. The closest I've come up with so far is:
0 40/5 14 * * MON-FRI
But this doesn't run at 15:00.
Is this possible to express with a Crontab pattern at all?
You need to split it into two expressions :
0 40-55/5 14 * * MON-FRI
0 0 15 * * MON-FRI
If you only want to have one expression and you are okay with the last trigger time in a day run one minute before, you can use :
0 40-55/5,59 14 * * MON-FRI
Spring internally use CronSequenceGenerator to generate the next trigger time for a cron expression. You can use it to verify a cron expression.
For example , to verify the next several trigger times of 0 40-55/5,59 14 * * MON-FRI:
CronSequenceGenerator generator = new CronSequenceGenerator("0 40-55/5,59 14 * * MON-FRI");
Date d = Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant());
for (int i = 0; i < 10; i++) {
d = generator.next(d);
System.out.println(d);
}
which will print out :
Tue Jun 02 14:40:00 HKT 2020
Tue Jun 02 14:45:00 HKT 2020
Tue Jun 02 14:50:00 HKT 2020
Tue Jun 02 14:55:00 HKT 2020
Tue Jun 02 14:59:00 HKT 2020
Wed Jun 03 14:40:00 HKT 2020
Wed Jun 03 14:45:00 HKT 2020
Wed Jun 03 14:50:00 HKT 2020
Wed Jun 03 14:55:00 HKT 2020
Wed Jun 03 14:59:00 HKT 2020
I'm parsing the Date that received from user query and another date from XML file.
When I want to get just the YEAR, it print 1 instead of year, by using:
calender.YEAR
This is the code:
Elements date = doc.getElementsByTag("DATE");
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date inputDate = dateFormat.parse(obj.tryParse(dateFrom));
Date xmlDate = dateFormat.parse(obj.tryParse(date.get(0).text()));
Calendar calInput = Calendar.getInstance();
Calendar calXml = Calendar.getInstance();
calInput.setTime(inputDate);
calXml.setTime(xmlDate);
System.out.println(calInput.YEAR +" "+ calXml.YEAR +" = "+calInput.getTime().toString()+"|||"+ calXml.getTime().toString());
if(calInput.YEAR != calXml.YEAR)
continue;
And this is a sample of the output:
Info: 1 1 = Wed Jan 01 00:00:00 IST 2003|||Tue Jan 01 00:00:00 IST 2002
Info: 1 1 = Wed Jan 01 00:00:00 IST 2003|||Tue Jan 01 00:00:00 IST 2002
Info: 1 1 = Wed Jan 01 00:00:00 IST 2003|||Tue Jan 01 00:00:00 IST 2002
Info: 1 1 = Wed Jan 01 00:00:00 IST 2003|||Tue Jan 01 00:00:00 IST 2002
Info: 1 1 = Wed Jan 01 00:00:00 IST 2003|||Tue Jan 01 00:00:00 IST 2002
Info: 1 1 = Wed Jan 01 00:00:00 IST 2003|||Tue Jan 01 00:00:00 IST 2002
Info: 1 1 = Wed Jan 01 00:00:00 IST 2003|||Tue Jan 01 00:00:00 IST 2002
Info: 1 1 = Wed Jan 01 00:00:00 IST 2003|||Tue Jan 01 00:00:00 IST 2002
Info: 1 1 = Wed Jan 01 00:00:00 IST 2003|||Tue Jan 01 00:00:00 IST 2002
Info: 1 1 = Wed Jan 01 00:00:00 IST 2003|||Tue Jan 01 00:00:00 IST 2002
Info: 1 1 = Wed Jan 01 00:00:00 IST 2003|||Sun Feb 01 00:00:00 IST 2004
Info: 1 1 = Wed Jan 01 00:00:00 IST 2003|||Mon Apr 01 00:00:00 IDT 2002
Info: 1 1 = Wed Jan 01 00:00:00 IST 2003|||Mon Apr 01 00:00:00 IDT 2002
Info: 1 1 = Wed Jan 01 00:00:00 IST 2003|||Mon Apr 01 00:00:00 IDT 2002
Info: 1 1 = Wed Jan 01 00:00:00 IST 2003|||Mon Apr 01 00:00:00 IDT 2002
Info: 1 1 = Wed Jan 01 00:00:00 IST 2003|||Mon Apr 01 00:00:00 IDT 2002
Info: 1 1 = Wed Jan 01 00:00:00 IST 2003|||Mon Apr 01 00:00:00 IDT 2002
Info: 1 1 = Wed Jan 01 00:00:00 IST 2003|||Mon Apr 01 00:00:00 IDT 2002
Info: 1 1 = Wed Jan 01 00:00:00 IST 2003|||Mon Apr 01 00:00:00 IDT 2002
calXml.YEAR should be written Calendar.YEAR, because it is a constant (public static final) value to be used as a parameter to methods like get(int field) and add(int field, int amount).
To get the year, call calXml.get(Calendar.YEAR).
The Answer by Andreas is correct.
java.time
Alternatively, you can use the new java.time framework built into Java 8 and later. The old date-time classes are a mess and should be avoided.
Convert from java.util.Date to java.time.Instant. An Instant is a moment on the timeline in UTC.
Instant instant = yourJavaUtilDate.toInstant();
Apply a time zone. Your Question's code implicitly depends on the JVM’s current default time zone. I suggest that it is better to explicitly specify your desired/expected time zone. The time zone is critical in determining a date and therefore the year.
ZoneId zoneId = ZoneId.of( "Asia/Kolkata" );
ZonedDateTime zdt = ZonedDateTime.ofInstant( instant , zoneId );
Now we can extract the year number.
int year = zdt.getYear();
Or more interesting, we can get a Year object.
Year year = Year.from( zdt );
Boolean isLeapYear = year.isLeap();
int yearNumber = year.getValue();
After a lot of debugging I narrowed my problem to the following snippet:
public static void calendarBug() {
for (int i=0 ; i<6 ; i++) {
Calendar c = Calendar.getInstance();
c.clear();
c.set(2015, 2, 27, i, 0);
System.out.println(c.getTime());
}
}
Running this gives the following output:
Fri Mar 27 00:00:00 IST 2015
Fri Mar 27 01:00:00 IST 2015
Fri Mar 27 03:00:00 IDT 2015
Fri Mar 27 03:00:00 IDT 2015
Fri Mar 27 04:00:00 IDT 2015
Fri Mar 27 05:00:00 IDT 2015
Does anyone know why does c.set(2015,2,27,2,0) returns 3AM instead of 2AM?
Think about that your are at time just at DST time, It moves forward one hour or moves back one hour without living this time portion.
So It is not bug, it is feature.
When you change the timezone which does not use DST (lets say Canada/East-Saskatchewan), you will see what you have expected.
Here is an example.
public static void calendarBug() {
for (int i=0 ; i<6 ; i++) {
Calendar c = Calendar.getInstance();
c.setTimeZone(TimeZone.getTimeZone("Canada/East-Saskatchewan"));
c.clear();
c.set(2015, 2, 27, i, 0);
System.out.println(c.getTime());
}
}
Fri Mar 27 08:00:00 EET 2015
Fri Mar 27 09:00:00 EET 2015
Fri Mar 27 10:00:00 EET 2015
Fri Mar 27 11:00:00 EET 2015
Fri Mar 27 12:00:00 EET 2015
Fri Mar 27 13:00:00 EET 2015
That is most likely a DST switchover for your timezone.
Mar 27 is the last Friday of March in 2015. This is the day when the DST switchover takes place in the Israel, Jordan, Syria, the West Bank etc.
See
Daylight Savings Time
Daylight Savings Time by Country
Daylight Saving Time Dates - 2014/2015
The problem is that the subtraction of the two dates leaves me double identical value.
From : Thu Nov 29 00:00:00 CET 2012
Value 121: To: Sat Mar 30 00:00:00 CET 2013
Value 122: To: Sun Mar 31 00:00:00 CET 2013
Value 122: To: Mon Apr 01 00:00:00 CEST 2013
Value 123: To: Tue Apr 02 00:00:00 CEST 2013
Long today = (To.getTime() - From.getTime()) / (1000 * 60 * 60 * 24);
Is it possible to somehow avoid that logically? (sorry for English)
So I'm following thenewboston's tutorials on Slick 2D. I'm loading the Image exactly as he said:
private org.newdawn.slick.Image playNow;
...
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
...
playNow = new org.newdawn.slick.Image("res/playNow.png");
This is my Eclipse project tree:
This is the output it gives me:
Tue Jul 03 09:31:53 PDT 2012 INFO:Slick Build #274
Tue Jul 03 09:31:53 PDT 2012 INFO:LWJGL Version: 2.8.5
Tue Jul 03 09:31:53 PDT 2012 INFO:OriginalDisplayMode: 1366 x 768 x 32 #60Hz
Tue Jul 03 09:31:53 PDT 2012 INFO:TargetDisplayMode: 640 x 360 x 0 #0Hz
Tue Jul 03 09:31:53 PDT 2012 INFO:Starting display 640x360
Tue Jul 03 09:31:53 PDT 2012 INFO:Use Java PNG Loader = true
Tue Jul 03 09:31:53 PDT 2012 INFO:Controllers not available
Exception in thread "main" java.lang.RuntimeException: Resource not found: res/playNow.png
at org.newdawn.slick.util.ResourceLoader.getResourceAsStream(ResourceLoader.java:69)
at org.newdawn.slick.opengl.InternalTextureLoader.getTexture(InternalTextureLoader.java:169)
at org.newdawn.slick.Image.<init>(Image.java:196)
at org.newdawn.slick.Image.<init>(Image.java:170)
at org.newdawn.slick.Image.<init>(Image.java:158)
at org.newdawn.slick.Image.<init>(Image.java:136)
at net.sourceforge.whowantsakookie.hamblaster.Menu.init(Menu.java:21)
at net.sourceforge.whowantsakookie.hamblaster.Game.initStatesList(Game.java:20)
at org.newdawn.slick.state.StateBasedGame.init(StateBasedGame.java:164)
at org.newdawn.slick.AppGameContainer.setup(AppGameContainer.java:390)
at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:314)
at net.sourceforge.whowantsakookie.hamblaster.Game.main(Game.java:31)
The tutorial is located here
I am doing this exactly as the tutorial said, and it works for him in the video. Thanks in advance :)
No your problem is that the "res" folder is in the same folder as the "src" file is in!
move your "res" folder containing all of your pictures INTO the "src/net.sourceforge.whowant/" folder/package in your program.
In your program you will load it by using
playNow = new org.newdawn.slick.Image("res/playNow.png");
Hope this helps! :)