applicationutilities.java

来自「SUN公司发布的SmartTicket 2.0蓝图」· Java 代码 · 共 224 行

JAVA
224
字号
/* * Copyright 2001, 2002, 2003 Sun Microsystems, Inc. All Rights Reserved. * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * - Redistribution in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * Neither the name of Sun Microsystems, Inc., the 'Java Smart Ticket * Sample Application', 'Java', 'Java'-based names, or the names of * contributors may be used to endorse or promote products derived from * this software without specific prior written permission. * Java Smart Ticket Sample Application 1.2 graphics and images may not * be reproduced in any form, in whole or in part, by any means without * prior written authorization of Sun and its licensors, if any. * This software is provided "AS IS," without a warranty of any kind. ALL * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND * ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES OR LIABILITIES * SUFFERED BY LICENSEE AS A RESULT OF OR RELATING TO USE, MODIFICATION * OR DISTRIBUTION OF THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL * SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, * OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. * You acknowledge that Software is not designed, licensed or intended * for use in the design, construction, operation or maintenance of any * nuclear facility. * $Id: ApplicationUtilities.java,v 1.2 2003/05/29 06:36:41 ro89390 Exp $ */package com.sun.j2me.blueprints.smartticket.client.midp.util;import java.util.*;final public class ApplicationUtilities {    private ApplicationUtilities() {}    public static String getCreditCardExpirationDate(int month, int year) {        StringBuffer buf = new StringBuffer();        switch (month) {        case Calendar.JANUARY:            buf.append("01");            break;        case Calendar.FEBRUARY:            buf.append("02");            break;        case Calendar.MARCH:            buf.append("03");            break;        case Calendar.APRIL:            buf.append("04");            break;        case Calendar.MAY:            buf.append("05");            break;        case Calendar.JUNE:            buf.append("06");            break;        case Calendar.JULY:            buf.append("07");            break;        case Calendar.AUGUST:            buf.append("08");            break;        case Calendar.SEPTEMBER:            buf.append("09");            break;        case Calendar.OCTOBER:            buf.append("10");            break;        case Calendar.NOVEMBER:            buf.append("11");            break;        case Calendar.DECEMBER:            buf.append("12");            break;        default:            throw new IllegalArgumentException();        }        buf.append('/');        String yearString = String.valueOf(year);        if (yearString.length() != 4) {            throw new IllegalArgumentException();        }         buf.append(yearString);        return buf.toString();    }     public static int getCreditCardExpirationMonth(String date) {        if (date.length() != 7 || date.indexOf('/') != 2) {            throw new IllegalArgumentException("Date not formatted properly");        }         try {            String monthString = date.substring(0, 2);            if (monthString.equals("01")) {                return Calendar.JANUARY;            } else if (monthString.equals("02")) {                return Calendar.FEBRUARY;            } else if (monthString.equals("03")) {                return Calendar.MARCH;            } else if (monthString.equals("04")) {                return Calendar.APRIL;            } else if (monthString.equals("05")) {                return Calendar.MAY;            } else if (monthString.equals("06")) {                return Calendar.JUNE;            } else if (monthString.equals("07")) {                return Calendar.JULY;            } else if (monthString.equals("08")) {                return Calendar.AUGUST;            } else if (monthString.equals("09")) {                return Calendar.SEPTEMBER;            } else if (monthString.equals("10")) {                return Calendar.OCTOBER;            } else if (monthString.equals("11")) {                return Calendar.NOVEMBER;            } else if (monthString.equals("12")) {                return Calendar.DECEMBER;            } else {                throw new IllegalArgumentException("Invalid date");            }         } catch (NumberFormatException nfe) {            throw new IllegalArgumentException(nfe.getMessage());        }     }     public static int getCreditCardExpirationYear(String date) {        if (date.length() != 7 || date.indexOf('/') != 2) {            throw new IllegalArgumentException("Date not formatted properly");        }         try {            return Integer.parseInt(date.substring(3));        } catch (NumberFormatException nfe) {            throw new IllegalArgumentException(nfe.getMessage());        }     }     public static String showTimeToString(int[] showTime) {        StringBuffer buf = new StringBuffer();        showTimeToStringBuffer(showTime, buf);        return buf.toString();    }     public static void showTimeToStringBuffer(int[] showTime,                                               StringBuffer buf) {        // Day.        if (showTime[0] == Calendar.MONDAY) {            buf.append("M ");        } else if (showTime[0] == Calendar.TUESDAY) {            buf.append("Tu ");        } else if (showTime[0] == Calendar.WEDNESDAY) {            buf.append("W ");        } else if (showTime[0] == Calendar.THURSDAY) {            buf.append("Th ");        } else if (showTime[0] == Calendar.FRIDAY) {            buf.append("F ");        } else if (showTime[0] == Calendar.SATURDAY) {            buf.append("Sa ");        } else if (showTime[0] == Calendar.SUNDAY) {            buf.append("Su ");        }         // Hours.        if (showTime[1] < 10) {            buf.append('0');            buf.append(showTime[1]);        } else {            buf.append(showTime[1]);        }         // Minutes.        if (showTime[2] < 10) {            buf.append('0');            buf.append(showTime[2]);        } else {            buf.append(showTime[2]);        }     } }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?