public static void main(String[] args) {
   Month birthMonth;
   String userEntry;
   int position;
   int comparison;
   Scanner input = new Scanner(System.in);
   for (Month mon : Month.values()) System.out.print(mon + " ");
   System.out.print("\n\nEnter the first three letters of " + "your birth month >> ");
   userEntry = input.nextLine().toUpperCase();
   birthMonth = Month.valueOf(userEntry);
   System.out.println("You entered " + birthMonth);
   position = birthMonth.ordinal();
   System.out.println(birthMonth + " is in position " + position);
   System.out.println("So its month number is " + (position + 1));
   comparison = birthMonth.compareTo(Month.JUN);
   if (comparison < 0)
     System.out.println(birthMonth + " is earlier in the year than " + Month.JUN);
   else if (comparison > 0)
     System.out.println(birthMonth + " is later in the year than " + Month.JUN);
   else System.out.println(birthMonth + " is " + Month.JUN);
 }
示例#2
0
  public static String formatDateTime(Context ctx, String date) {
    /*
     * Date as: Tue Sep 03 19:12:43 +0800 2013
     */
    String[] times = date.split(" ");

    Loge.i("Origin Data : " + date);
    Loge.i("times.length : " + times.length);

    StringBuffer sbDate = new StringBuffer("");

    if (times.length < 6) {
      return null;
    }

    Date finalDate = new Date();

    Month mKey = Month.valueOf(times[1]);

    switch (mKey) {
      case Jan:
        finalDate.setMonth(1);
        break;
      case Feb:
        finalDate.setMonth(2);
        break;
      case Apr:
        finalDate.setMonth(3);
        break;
      case May:
        finalDate.setMonth(4);
        break;
      case Mar:
        finalDate.setMonth(5);
        break;
      case Jun:
        finalDate.setMonth(6);
        break;
      case Jul:
        finalDate.setMonth(7);
        break;
      case Aug:
        finalDate.setMonth(8);
        break;
      case Sep:
        finalDate.setMonth(9);
        break;
      case Oct:
        finalDate.setMonth(10);
        break;
      case Nov:
        finalDate.setMonth(11);
        break;
      case Dec:
        finalDate.setMonth(12);
        break;
      default:
        break;
    }

    String day = times[2];
    Loge.i("Data day : " + day);
    if (day.startsWith("0")) {
      day = day.substring(1);
    }

    finalDate.setDate(Integer.parseInt(day));

    String[] dayTimes = times[3].split(":");

    for (int i = 0; i < dayTimes.length; i++) {
      switch (i) {
        case 0:
          finalDate.setHours(Integer.parseInt(dayTimes[0]));
          break;
        case 1:
          finalDate.setMinutes(Integer.parseInt(dayTimes[1]));
          break;
        case 2:
          finalDate.setSeconds(Integer.parseInt(dayTimes[2]));
          break;
        default:
          break;
      }
    }

    finalDate.setYear(Integer.valueOf(times[5]));

    sbDate.append(finalDate.getMonth());
    sbDate.append(ctx.getResources().getString(R.string.month));
    sbDate.append(finalDate.getDate());
    sbDate.append(ctx.getResources().getString(R.string.day));
    sbDate.append(" ");
    sbDate.append(finalDate.getHours());
    sbDate.append(":");
    if (finalDate.getMinutes() == 0) {
      sbDate.append("00");
    } else {
      sbDate.append(finalDate.getMinutes());
    }

    finalDate = null;

    Loge.i("Data String : " + sbDate.toString());

    return sbDate.toString();
  }