Here you can find the source of formatDate(Date date, boolean time, boolean csv)
Parameter | Description |
---|
public static String formatDate(Date date, boolean time, boolean csv)
//package com.java2s; /******************************************************************************* * Copyright 2014 Miami-Dade County// w w w.j a v a 2 s . com * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. ******************************************************************************/ import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class Main { public static final String emptyField = "----"; public static final String blankField = ""; public static String formatDate(String strDate, boolean time, boolean csv) { SimpleDateFormat sdf = null; if (time) sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS"); else if (strDate.contains("/")) sdf = new SimpleDateFormat("MM/dd/yyyy"); else if (strDate.contains(",")) sdf = new SimpleDateFormat("MMM dd, yyyy"); else sdf = new SimpleDateFormat("yyyy-MM-dd"); try { if (!strDate.trim().isEmpty()) return formatDate(sdf.parse(strDate), time, csv); else return blankField; } catch (ParseException e) { e.printStackTrace(); return blankField; } } /** * returns the date in string format. * @param java.util.Date date: If null, then returns an empty string. * @param boolean time: If true, returns date timestamp and only date if false * @return */ public static String formatDate(Date date, boolean time, boolean csv) { StringBuilder sb = new StringBuilder(""); if (time) { if (csv) sb.append("MMM dd yyyy hh:mm a"); else sb.append("MMM dd, yyyy hh:mm a"); } else { if (csv) sb.append("MMM dd yyyy"); else sb.append("MMM dd, yyyy"); } SimpleDateFormat sdf = new SimpleDateFormat(sb.toString()); if (date != null) return sdf.format(date); else return emptyField; } /** * returns the date in String MMddyyyy format. * if date is null then returns an empty string. * @param date * @return */ public static String formatDate(Date date) { SimpleDateFormat sdf = null; if (date != null) { sdf = new SimpleDateFormat("MMddyyyy"); return sdf.format(date); } else return emptyField; } }