Example usage for java.text SimpleDateFormat getCalendar

List of usage examples for java.text SimpleDateFormat getCalendar

Introduction

In this page you can find the example usage for java.text SimpleDateFormat getCalendar.

Prototype

public Calendar getCalendar() 

Source Link

Document

Gets the calendar associated with this date/time formatter.

Usage

From source file:Main.java

public static long getCurrentGmtTime(String format) {
    final SimpleDateFormat dateFormatGmt = new SimpleDateFormat(format, Locale.getDefault());
    dateFormatGmt.setTimeZone(TimeZone.getTimeZone("GMT"));
    return dateFormatGmt.getCalendar().getTimeInMillis();
}

From source file:jp.terasoluna.fw.util.DateUtil.java

/**
 * java.util.Date?????//from w  w  w. ja va  2  s  . co m
 * ??
 *
 * <p>
 *  ApplicationResources.properties
 *  ?????????
 *  ???????<br>
 *  ??????????
 *  ??
 *  <strong> ApplicationResources.properties ?
 *  ?</strong><br>
 *  <code><pre>
 *  wareki.gengo.0.name = ?
 *  wareki.gengo.0.roman = H
 *  wareki.gengo.0.startDate = 1989/01/08
 *  wareki.gengo.1.name = 
 *  wareki.gengo.1.roman = S
 *  wareki.gengo.1.startDate = 1926/12/25
 *  wareki.gengo.2.name = 
 *  wareki.gengo.2.roman = T
 *  wareki.gengo.2.startDate = 1912/07/30
 *  wareki.gengo.3.name = 
 *  wareki.gengo.3.roman = M
 *  wareki.gengo.3.startDate = 1868/09/04
 *  </pre></code>
 * </p>
 *
 * <strong></strong><br>
 * <p>??java.text.SimpleDateFormat ?
 * <i></i> ?????????
 * ? SimpleDateFormat ??
 * </p>
 *
 * <div width="90%" align="center">
 *  <table border="1">
 *   <tr>
 *    <th>?</th>
 *    <th><code>&nbsp;SimpleDateFormat</code>&nbsp;</th>
 *    <th><code>&nbsp;dateToWarekiString()</code>&nbsp;</th>
 *   </tr>
 *   <tr>
 *    <td>G</td>
 *    <td align="left"><br><br><br>AD</td>
 *    <td align="left">?<br><br>
 *                     <br>
 *                    4???<br>
 *                     ????<br>
 *                     3???<br>
 *                     M?T?S?H</td>
 *   </tr>
 *   <tr>
 *    <td>y</td>
 *    <td align="left"><br><br><br>2002</td>
 *    <td align="left"><br><br><br>14</td>
 *   </tr>
 *   <tr>
 *    <td>E</td>
 *    <td align="left"><br><br><br>Tuesday</td>
 *    <td align="left"><br><br>
 *                     <br>
 *                    4???<br>
 *                     ???<br>
 *                     3???<br>
 *                     ???</td>
 *   </tr>
 * </table>
 * </div>
 *
 * <p>?????E????? SimpleDateFotmat
 * ???? &quot;ja&quot;
 * ???????</p>
 *
 * <p>????????????getWarekiGengoName()?
 * getWarekiGengoRoman()?getWarekiYear() ?????
 * ????????AplicationResources ?
 * ????</p>
 *
 * <p><code><pre>
 * wareki.gengo.<i>ID</i>.name=<i>???</i>
 * wareki.gengo.<i>ID</i>.roman=<i>??</i>
 * wareki.gengo.<i>ID</i>.startDate=<i>?:yyyy/MM/dd?</i>
 * </pre></code></p>
 *
 * <p>ID????????????????
 * ???</p>
 *
 * @param format 
 * @param date ???
 * @return ?????
 */
public static String dateToWarekiString(String format, java.util.Date date) {

    // SimpleDateFormat?????'G'???'y'?
    // ???
    StringBuilder sb = new StringBuilder();
    boolean inQuote = false; // ???????
    char prevCh = 0;
    int count = 0;
    for (int i = 0; i < format.length(); i++) {
        char ch = format.charAt(i);
        if (ch != prevCh && count > 0) {
            if (prevCh == 'G' && count >= 4) {
                sb.append(getWarekiGengoName(date));
            } else if (prevCh == 'G') {
                // ???????????
                sb.append('\'');
                sb.append(getWarekiGengoRoman(date));
                sb.append('\'');
            } else if (prevCh == 'y') {
                sb.append(getWarekiYear(date));
            }
            count = 0;
        }

        if (ch == '\'') {
            sb.append('\'');
            inQuote = !inQuote;
        } else if (!inQuote && (ch == 'G' || ch == 'y')) {
            // ch??????????
            // ??
            prevCh = ch;
            ++count;
        } else {
            // ????????
            sb.append(ch);
        }
    }

    // ????
    if (count > 0) {
        if (prevCh == 'G' && count >= 4) {
            sb.append(getWarekiGengoName(date));
        } else if (prevCh == 'G') {
            sb.append('\'');
            sb.append(getWarekiGengoRoman(date));
            sb.append('\'');
        } else if (prevCh == 'y') {
            sb.append(getWarekiYear(date));
        }
    }

    SimpleDateFormat sdf = new SimpleDateFormat(sb.toString(), Locale.JAPAN);

    sdf.getCalendar().setLenient(false);
    sdf.setLenient(false);
    return sdf.format(date);
}

From source file:ymanv.forex.provider.impl.EuropeanCentralBank.java

private List<RateEntity> getRates(String response, boolean usdBase) throws IOException {
    SimpleDateFormat sf = new SimpleDateFormat(DATE_FORMAT_STRNG);
    Calendar sfCalendar = sf.getCalendar();
    sfCalendar.setTimeZone(TZ);/* www. j  av  a2s  .c o  m*/

    List<RateEntity> rates = new ArrayList<>();

    String cleanResponse = response.substring(response.indexOf("<Cube>"), response.lastIndexOf("</Cube>") + 7);
    try {
        JAXBContext context = JAXBContext.newInstance(Result.class);
        Unmarshaller un = context.createUnmarshaller();
        Result res = (Result) un.unmarshal(new StringReader(cleanResponse));

        for (Day d : res.getDays()) {
            Date date = d.getDate();
            sf.parse(sf.format(date));
            sfCalendar.set(Calendar.HOUR, 15);
            date = sf.getCalendar().getTime();

            if (!usdBase) {
                for (Rate r : d.getRates()) {
                    rates.add(new RateEntity(BASE_CURRENCY, r.getTocur(), new BigDecimal(r.getValue()), date));
                }
            } else {
                BigDecimal usdToEurValue = null;

                for (Rate r : d.getRates()) {
                    if (USD.equals(r.getTocur())) {
                        usdToEurValue = invert(new BigDecimal(r.getValue()));
                        break;
                    }
                }

                for (Rate r : d.getRates()) {
                    if (USD.equals(r.getTocur())) {
                        rates.add(new RateEntity(USD, BASE_CURRENCY, usdToEurValue, date));
                    } else {
                        rates.add(new RateEntity(USD, r.getTocur(),
                                new BigDecimal(r.getValue()).multiply(usdToEurValue), date));
                    }
                }
            }
        }

        return rates;
    } catch (ParseException | JAXBException e) {
        throw new IOException(e);
    }
}

From source file:org.montanafoodhub.base.get.AdHub.java

protected List<Ad> readFromFile(Context context) {
    List<Ad> myAdArr = new ArrayList<Ad>();
    try {//from  w  ww .  ja v  a2 s . c  o m
        // getItem the time the file was last changed here
        File myFile = new File(context.getFilesDir() + "/" + fileName);
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        String lastRefreshTSStr = sdf.format(myFile.lastModified());
        Log.w(HubInit.logTag, "Using file (" + fileName + ") last modified on : " + lastRefreshTSStr);
        lastRefreshTS = sdf.getCalendar();

        // create products from the file here
        InputStream inputStream = context.openFileInput(fileName);
        if (inputStream != null) {
            parseCSV(myAdArr, inputStream);
        }
        inputStream.close();
    } catch (FileNotFoundException e) {
        Log.e(HubInit.logTag, "File  (" + fileName + ") not found: " + e.toString());
    } catch (IOException e) {
        Log.e(HubInit.logTag, "Can not read file  (" + fileName + ") : " + e.toString());
    }
    Log.w(HubInit.logTag, "Number of Ad loaded: " + myAdArr.size());
    return myAdArr;
}

From source file:org.montanafoodhub.base.get.OrderHub.java

protected List<Order> readFromFile(Context context) {
    List<Order> myOrderArr = new ArrayList<Order>();
    try {/* w  w w  .  jav  a 2  s. c  o m*/
        // getItem the time the file was last changed here
        File myFile = new File(context.getFilesDir() + "/" + fileName);
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        String lastRefreshTSStr = sdf.format(myFile.lastModified());
        Log.w(HubInit.logTag, "Using file (" + fileName + ") last modified on : " + lastRefreshTSStr);
        lastRefreshTS = sdf.getCalendar();

        // create products from the file here
        InputStream inputStream = context.openFileInput(fileName);
        if (inputStream != null) {
            parseCSV(myOrderArr, inputStream);
        }
        inputStream.close();
    } catch (FileNotFoundException e) {
        Log.e(HubInit.logTag, "File  (" + fileName + ") not found: " + e.toString());
    } catch (IOException e) {
        Log.e(HubInit.logTag, "Can not read file  (" + fileName + ") : " + e.toString());
    } catch (ParseException pe) {
        Log.e(HubInit.logTag, "Can't parse Order date  (" + fileName + ") : " + pe.toString());
    }
    Log.w(HubInit.logTag, "Number of orders loaded: " + myOrderArr.size());
    return myOrderArr;
}

From source file:org.montanafoodhub.base.get.CertificationHub.java

protected HashMap<String, Certification> readFromFile(Context context) {
    HashMap<String, Certification> myCertificationMap = new HashMap<String, Certification>();
    try {//w w  w. j ava 2 s  .c  o m
        // getItem the time the file was last changed here
        File myFile = new File(context.getFilesDir() + "/" + fileName);
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        String lastRefreshTSStr = sdf.format(myFile.lastModified());
        Log.w(HubInit.logTag, "Using file (" + fileName + ") last modified on : " + lastRefreshTSStr);
        lastRefreshTS = sdf.getCalendar();

        // create products from the file here
        InputStream inputStream = context.openFileInput(fileName);
        if (inputStream != null) {
            parseCSV(myCertificationMap, inputStream);
        }
        inputStream.close();
    } catch (FileNotFoundException e) {
        Log.e(HubInit.logTag, "File  (" + fileName + ") not found: " + e.toString());
    } catch (IOException e) {
        Log.e(HubInit.logTag, "Can not read file  (" + fileName + ") : " + e.toString());
    }
    Log.w(HubInit.logTag, "Number of certifications loaded: " + myCertificationMap.size());
    return myCertificationMap;
}

From source file:org.montanafoodhub.base.get.ItemHub.java

protected HashMap<String, Item> readFromFile(Context context) {
    HashMap<String, Item> myItemMap = new HashMap<String, Item>();
    try {/*  www  .j  a  va  2s. com*/
        // getItem the time the file was last changed here
        File myFile = new File(context.getFilesDir() + "/" + fileName);
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        String lastRefreshTSStr = sdf.format(myFile.lastModified());
        Log.w(HubInit.logTag, "Using file (" + fileName + ") last modified on : " + lastRefreshTSStr);
        lastRefreshTS = sdf.getCalendar();

        // create products from the file here
        InputStream inputStream = context.openFileInput(fileName);
        if (inputStream != null) {
            parseCSV(myItemMap, inputStream);
        }
        inputStream.close();
    } catch (FileNotFoundException e) {
        Log.e(HubInit.logTag, "File  (" + fileName + ") not found: " + e.toString());
    } catch (IOException e) {
        Log.e(HubInit.logTag, "Can not read file  (" + fileName + ") : " + e.toString());
    }
    Log.w(HubInit.logTag, "Number of items loaded: " + myItemMap.size());
    return myItemMap;
}

From source file:org.montanafoodhub.base.get.BuyerHub.java

protected HashMap<String, Buyer> readFromFile(Context context) {
    HashMap<String, Buyer> myBuyerMap = new HashMap<String, Buyer>();
    try {//  w w  w  . j  a  v  a  2s  .c  o m
        // getItem the time the file was last changed here
        File myFile = new File(context.getFilesDir() + "/" + fileName);
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        String lastRefreshTSStr = sdf.format(myFile.lastModified());
        Log.w(HubInit.logTag, "Using file (" + fileName + ") last modified on : " + lastRefreshTSStr);
        lastRefreshTS = sdf.getCalendar();

        // create products from the file here
        InputStream inputStream = context.openFileInput(fileName);
        if (inputStream != null) {
            parseCSV(myBuyerMap, inputStream);
        }
        inputStream.close();
    } catch (FileNotFoundException e) {
        Log.e(HubInit.logTag, "File  (" + fileName + ") not found: " + e.toString());
    } catch (IOException e) {
        Log.e(HubInit.logTag, "Can not read file  (" + fileName + ") : " + e.toString());
    }
    Log.w(HubInit.logTag, "Number of buyers loaded: " + myBuyerMap.size());
    return myBuyerMap;
}

From source file:org.montanafoodhub.base.get.ProducerHub.java

protected HashMap<String, Producer> readFromFile(Context context) {
    HashMap<String, Producer> myProducerMap = new HashMap<String, Producer>();
    try {/*  w  w  w. j av  a  2s  . c o  m*/
        // getItem the time the file was last changed here
        File myFile = new File(context.getFilesDir() + "/" + fileName);
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        String lastRefreshTSStr = sdf.format(myFile.lastModified());
        Log.w(HubInit.logTag, "Using file (" + fileName + ") last modified on : " + lastRefreshTSStr);
        lastRefreshTS = sdf.getCalendar();

        // create products from the file here
        InputStream inputStream = context.openFileInput(fileName);
        if (inputStream != null) {
            parseCSV(myProducerMap, inputStream);
        }
        inputStream.close();
    } catch (FileNotFoundException e) {
        Log.e(HubInit.logTag, "File  (" + fileName + ") not found: " + e.toString());
    } catch (IOException e) {
        Log.e(HubInit.logTag, "Can not read file  (" + fileName + ") : " + e.toString());
    }
    Log.w(HubInit.logTag, "Number of producers loaded: " + myProducerMap.size());
    return myProducerMap;
}

From source file:org.montanafoodhub.base.get.InitHub.java

protected void readFromFile(Context context) {
    try {/*w w w  .ja v  a 2  s  .c  o  m*/
        // getItem the time the file was last changed here
        File myFile = new File(context.getFilesDir() + "/" + fileName);
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        String lastRefreshTSStr = sdf.format(myFile.lastModified());
        Log.w(HubInit.logTag, "Using file (" + fileName + ") last modified on : " + lastRefreshTSStr);
        lastRefreshTS = sdf.getCalendar();

        // *** last line in the init wins!!!
        InputStream inputStream = context.openFileInput(fileName);
        if (inputStream != null) {
            parseCSV(inputStream);
        }
        inputStream.close();
    } catch (FileNotFoundException e) {
        Log.e(HubInit.logTag, "File  (" + fileName + ") not found: " + e.toString());
    } catch (IOException e) {
        Log.e(HubInit.logTag, "Can not read file  (" + fileName + ") : " + e.toString());
    }
    Log.w(HubInit.logTag, "Hub name initialized to: " + HubInit.getHubName());
}