Week of Day String to INT

To change a Day of the week like Monday, Tuesday and so on to integer the use the following code:

  public int dayNameComparison(String dayName) {
           java.text.DateFormatSymbols objDaySymbol = new java.text.DateFormatSymbols();
           String symbolDayNames[] = objDaySymbol.getWeekdays();
           for (int countDayname = 0; countDayname < symbolDayNames.length;countDayname++) {
                if (dayName.equalsIgnoreCase(symbolDayNames[countDayname])) {
                      return countDayname;
                   }
               }
             return 0;
          }

 

This way when u pass the day as parameter the you will get the output as integer. This output can be used directly in Calendar class as input :

 

public static void main(String[] args) {
         TestMouse testMouse = new TestMouse();
         int i = testMouse.dayNameComparison("Tuesday");
         System.out.println("" + i);
         java.util.Calendar calendar = java.util.Calendar.getInstance();
         calendar.set(java.util.Calendar.DAY_OF_WEEK, i);
         System.out.println(new java.text.SimpleDateFormat("EEEE").format(calendar.getTime()));//Prints: Tuesday
}

xsd:date maps to java.util.Calendar

My schema has an element of type xs:date, which jaxb maps to a java.util.Calendar. If I create a Calendar object with Calendar.getInstance(), it marshalls to “2003-11-24-05:00”.

How can I get it to marshall to just “2003-11-24”?

SOLUTION:
Write a converter class (see MyConverter below) and added an annotation/appinfo to the xml schema, also shown below.

public class MyConverter
{
static final SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
static public String printCalendar(Calendar c)
{
return df.format(c.getTime());
}

static public Calendar parseCalendar(String c) throws ParseException
{
Date d = df.parse(c);
Calendar cal = Calendar.getInstance();
cal.setTime(d);
return cal;
}
}




<jaxb:globalBindings
<jaxb:javaType name="java.util.Calendar" xmlType="xsd:date"
printMethod="MyConverter.printCalendar"
parseMethod="MyConverter.parseCalendar"
/>



...

Source : https://forums.oracle.com/forums/thread.jspa?threadID=1624090