Example usage for java.text DateFormat format

List of usage examples for java.text DateFormat format

Introduction

In this page you can find the example usage for java.text DateFormat format.

Prototype

public final String format(Date date) 

Source Link

Document

Formats a Date into a date-time string.

Usage

From source file:com.hangum.tadpole.engine.query.sql.TadpoleSystem_UserQuery.java

/**
 * get login history/*from  w w w.  j  a  v  a  2  s .co  m*/
 * 
 * @param strEmail
 * @param startTime
 * @param endTime
 */
public static List<UserLoginHistoryDAO> getLoginHistory(String strEmail, long startTime, long endTime)
        throws TadpoleSQLManagerException, SQLException {
    SqlMapClient sqlClient = TadpoleSQLManager.getInstance(TadpoleSystemInitializer.getUserDB());

    Map<String, Object> queryMap = new HashMap<String, Object>();
    queryMap.put("email", strEmail);

    if (ApplicationArgumentUtils.isDBServer()) {
        Date dateSt = new Date(startTime);
        DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
        queryMap.put("startTime", formatter.format(dateSt));

        Date dateEd = new Date(endTime);
        queryMap.put("endTime", formatter.format(dateEd));
    } else {
        Date dateSt = new Date(startTime);
        DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");

        queryMap.put("startTime", formatter.format(dateSt));

        Date dateEd = new Date(endTime);
        queryMap.put("endTime", formatter.format(dateEd));
    }

    return (List<UserLoginHistoryDAO>) sqlClient.queryForList("getLoginHistory", queryMap);
}

From source file:org.openmrs.module.pcslabinterface.PcsLabInterfaceUtil.java

/**
 * Gets an out File object. If date is not provided, the current timestamp
 * is used. If user is not provided, the user id is not put into the
 * filename. Assumes dir is already created
 * /*from  w w  w  . j av  a2s  .co m*/
 * @param dir
 *            directory to make the random filename in
 * @param date
 *            optional Date object used for the name
 * @param user
 *            optional User creating this file object
 * @return file new file that is able to be written to
 */
public static File getOutFile(File dir, Date date, User user) {

    File outFile;
    do {
        // format to print date in filename
        DateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd-HHmm-ssSSS");

        // use current date if none provided
        if (date == null)
            date = new Date();

        StringBuilder filename = new StringBuilder();

        // the start of the filename is the time so we can do some sorting
        filename.append(dateFormat.format(date));

        // insert the user id if they provided it
        if (user != null) {
            filename.append("-");
            filename.append(user.getUserId());
            filename.append("-");
        }

        // the end of the filename is a random number between 0 and 10000
        filename.append((int) (Math.random() * 10000));
        filename.append(".txt");

        outFile = new File(dir, filename.toString());

        // set to null to avoid very minimal possibility of an infinite loop
        date = null;

    } while (outFile.exists());

    return outFile;
}

From source file:com.jaspersoft.jasperserver.war.common.JasperServerUtil.java

public static String formatDate(MessageSource messages, Date date, TimeZone timeZone) {
    DateFormat format = createCalendarDateTimeFormat(messages);
    format.setTimeZone(timeZone);//from w  w  w .  j a  va  2  s.  com
    return format.format(date);
}

From source file:gov.wa.wsdot.cms.utils.Migration.java

/**
 * Calculate number of days since the 1900 epoch.
 * /*from w w  w. j  a va 2s .  c o m*/
 * @param daysSinceEpoch days since epoch in milliseconds
 * @return converted date in the form "Tue Apr 09 07:56:37 PDT 2013"
 */
public static String convertDays(double daysSinceEpoch) {
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
    dateFormat.setTimeZone(TimeZone.getTimeZone("GMT")); // Set TimeZone to UTC
    Calendar startDate = Calendar.getInstance();
    startDate.set(1899, 11, 31, 16, 0, 0);
    long MILLIS_PER_DAY = 86400000;
    long createdWhen = (long) (daysSinceEpoch * MILLIS_PER_DAY + startDate.getTimeInMillis())
            - 2 * MILLIS_PER_DAY;

    return dateFormat.format(new Date(createdWhen));
}

From source file:com.clustercontrol.jobmanagement.util.ParameterUtil.java

/**
 *
 * ?????/*w  w w .  j a v  a 2s  .  co m*/
 *
 * @param paramId ID
 * @param sessionId ID
 * @param jobSessionEntity 
 * @return 
 * @throws JobInfoNotFound
 */
private static String getJobSessionValue(String paramId, String sessionId, JobSessionEntity jobSessionEntity)
        throws JobInfoNotFound {

    m_log.debug("getJobSessionValue() start paramId=" + paramId + ",sessionId=" + sessionId);
    String ret = null;

    if (paramId.equals(SystemParameterConstant.SESSION_ID)) {
        // ID
        ret = sessionId;
    } else {
        if (jobSessionEntity == null) {
            HinemosEntityManager em = new JpaTransactionManager().getEntityManager();

            // ??
            jobSessionEntity = em.find(JobSessionEntity.class, sessionId, ObjectPrivilegeMode.READ);
            if (jobSessionEntity == null) {
                JobInfoNotFound je = new JobInfoNotFound(
                        "JobSessionEntity.findByPrimaryKey" + ", sessionId = " + sessionId);
                m_log.info(
                        "getJobParameterValue() : " + je.getClass().getSimpleName() + ", " + je.getMessage());
                je.setSessionId(sessionId);
                throw je;
            }
        }

        if (paramId.equals(SystemParameterConstant.START_DATE)) {
            // 
            DateFormat df = DateFormat.getDateTimeInstance();
            df.setTimeZone(HinemosTime.getTimeZone());
            ret = df.format(jobSessionEntity.getScheduleDate());

        } else if (paramId.equals(SystemParameterConstant.TRIGGER_TYPE)) {
            // ?
            Locale locale = NotifyUtil.getNotifyLocale();
            ret = Messages.getString(
                    JobTriggerTypeConstant.typeToMessageCode(jobSessionEntity.getTriggerType()), locale);

        } else if (paramId.equals(SystemParameterConstant.TRIGGER_INFO)) {
            // ?
            ret = jobSessionEntity.getTriggerInfo();

        }
    }
    m_log.debug("getJobSessionValue() end paramId=" + paramId + ",sessionId=" + sessionId + ",value=" + ret);
    return ret;
}

From source file:com.icesoft.tutorial.TimeZoneBean.java

public static String formatCurrentTime(DateFormat dateFormat) {
    Calendar cal = dateFormat.getCalendar();
    cal.setTimeInMillis(System.currentTimeMillis());
    return dateFormat.format(cal.getTime());
}

From source file:foam.zizim.android.Util.java

/**
 * Format date into more readable format.
 * //  www.  j av  a  2 s.c om
 * @param  date - the date to be formatted.
 * @return String
 */
public static String formatDate(String fromFormat, String date, String toFormat) {
    String formatted = "";

    DateFormat formatter = new SimpleDateFormat(fromFormat, Locale.ENGLISH);
    try {
        Date dateStr = formatter.parse(date);
        formatted = formatter.format(dateStr);
        Date formatDate = formatter.parse(formatted);
        formatter = new SimpleDateFormat(toFormat);
        formatted = formatter.format(formatDate);

    } catch (ParseException e) {

        e.printStackTrace();
    }
    return formatted;
}

From source file:com.infovity.iep.loader.util.SupplierLoaderUtil.java

public static ArrayList<String[]> getDataFromFile(File inputFile, String sheetName) {
    ArrayList<String[]> data = new ArrayList<String[]>();
    File selectedFile = null;//from w  ww  .j a  v a2s. c  om
    FileInputStream fis = null;
    ;
    XSSFWorkbook workbook = null;
    //inputFile = new File("C:\\Users\\INFOVITY-USER-029\\Desktop\\DataLoader\\Consolidated Supplier Data Capture Template v4.0.xlsx");
    boolean sheetFound = false;
    XSSFSheet sheet = null;
    try {

        int sheetIndex = -1;
        fis = new FileInputStream(inputFile);
        workbook = new XSSFWorkbook(fis);

        int noOfSheets = workbook.getNumberOfSheets();
        for (int i = 0; i < noOfSheets; i++) {
            sheet = workbook.getSheetAt(i);
            if (sheet.getSheetName().equals(sheetName)) {
                sheetFound = true;
                sheetIndex = i;
                selectedFile = inputFile;
                break;
            }
        }
        XSSFWorkbook myWorkBook;

        try {
            myWorkBook = new XSSFWorkbook(selectedFile);
            // Return first sheet from the XLSX workbook
            // XSSFSheet mySheet = myWorkBook.getSheetAt(0);
            // Get iterator to all the rows in current sheet
            Iterator<Row> rowIterator = sheet.iterator();
            String query;
            String[] values = null;
            // Traversing over each row of XLSX file
            while (rowIterator.hasNext()) {
                Row row = rowIterator.next();
                if (rowHasData(row) && (row.getRowNum() >= 9)) {
                    int endColumn = row.getLastCellNum();
                    int startColumn = row.getFirstCellNum();
                    // For each row, iterate through each columns
                    values = new String[endColumn + 2];
                    for (int i = startColumn; i < endColumn; i++) {
                        String cellValue;
                        Cell cell = row.getCell(i);
                        values[0] = Integer.toString(row.getRowNum() + 1);
                        if (cell != null) {
                            if (cell.getCellType() == cell.CELL_TYPE_NUMERIC
                                    && DateUtil.isCellDateFormatted(cell)) {
                                DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
                                Date cellDateValue = cell.getDateCellValue();
                                cellValue = df.format(cellDateValue);
                            } else {
                                cell.setCellType(cell.CELL_TYPE_STRING);
                                cellValue = cell.getStringCellValue().replaceAll("'", "");
                            }
                            if (!"".equals(cellValue) && cellValue != null) {
                                values[i + 1] = cellValue;
                            } else if (cellValue.isEmpty() || "".equals(cellValue)) {
                                values[i + 1] = "";
                            }
                        } else {
                            values[i + 1] = "";
                        }
                    }
                    data.add(values);
                }

            }
        } catch (InvalidFormatException ex) {
            Logger.getLogger(SupplierLoaderUtil.class.getName()).log(Level.ERROR, null, ex);
        }
    } catch (IOException ex) {
        Logger.getLogger(SupplierLoaderUtil.class.getName()).log(Level.ERROR, null, ex);
    } finally {
        try {
            fis.close();
            workbook.close();
        } catch (IOException ex) {
            Logger.getLogger(SupplierLoaderUtil.class.getName()).log(Level.ERROR, null, ex);
        }
    }

    return data;
}

From source file:com.nubits.nubot.utils.Utils.java

/**
 * @return/*from   w  w w  .ja  va2 s.  co  m*/
 */
public static String getTimestampString() {
    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    Date date = new Date();
    return dateFormat.format(date);
}

From source file:Main.java

public static String ArrivalTime(long arrivalMilliseconds, String timeformat) {

    DateFormat formatter;
    Date convertedDate = null;//from   www  .  java 2 s  .c o m

    Calendar calendarToday = Calendar.getInstance();
    calendarToday.setTimeInMillis(arrivalMilliseconds);

    String Time = String
            .valueOf(calendarToday.get(Calendar.HOUR_OF_DAY) + ":" + calendarToday.get(Calendar.MINUTE));
    formatter = new SimpleDateFormat("HH:mm");
    try {
        convertedDate = (Date) formatter.parse(Time);

    } catch (ParseException e) {
        //e.printStackTrace();
    }

    if (timeformat.equals("24"))
        return formatter.format(convertedDate);
    else
        return ampmChanger(formatter.format(convertedDate));

}