Note that there are some explanatory texts on larger screens.

plurals
  1. POA very simple java code (to get date of a week day), but Very Strange result I get, WHY?
    primarykey
    data
    text
    <p>I have the following java code to get the date of a specific week day:</p> <pre><code>SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); Calendar cal = Calendar.getInstance(); cal.setTime(new Date()); cal.set(Calendar.YEAR, 2010); cal.set(Calendar.WEEK_OF_YEAR, 37); //week 37 of year 2010 cal.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY); System.out.println("date="+sdf.format(cal.getTime())); </code></pre> <p>When I put this code in a <code>main(String[] args)</code>method, like the following:</p> <pre><code>import java.util.*; import java.lang.*; import java.text.SimpleDateFormat; public class test{ public static void main(String[] args){ /** get dates from a known week ID **/ SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); Calendar cal = Calendar.getInstance(); cal.setTime(new Date()); cal.set(Calendar.YEAR, 2010); cal.set(Calendar.WEEK_OF_YEAR, 37); cal.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY); System.out.println("date="+sdf.format(cal.getTime())); } } </code></pre> <p>and run it, I get the correct result which is <code>date=09/09/2010</code>. There is no problem.</p> <p><strong>HOWEVER...</strong></p> <p>When I put this code in a function of a Class, like the following:</p> <pre><code>Public Class MyService{ MyService(){} ... ... public String getDateOfWeekDay(int weekId, int year, int weekDay){ //weekId = 37; year=2010; weekDay = Calendar.THURSDAY SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); Calendar cal = Calendar.getInstance(); cal.setTime(new Date()); cal.set(Calendar.YEAR, year); cal.set(Calendar.WEEK_OF_YEAR, weekId); cal.set(Calendar.DAY_OF_WEEK, weekDay); //I am developing android app. so I use Log to printout logPrinter.println("date="+sdf.format(cal.getTime())); return sdf.format(cal.getTime()); } } </code></pre> <p>in another class MainClass, MainClass will invoke this service function like following:</p> <pre><code>Public Class MainClass{ MyService myService = new MyService(); myService.getDateOfWeekDay(37,2010,Calendar.THURSDAY); } </code></pre> <p>But the result returned is always the date of the <strong>current week's thursday (date=14/10/2010)</strong>, not the Thursday of the week which I specified (week 37, year 2010, Thursday). WHY???? I use exactly the same java code to get the date of the specific week day, only used it in different ways, why the result is different???? I can not understand this...Anybody can explain to me?? </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload