Java Date Create toDateList(Collection list)

Here you can find the source of toDateList(Collection list)

Description

Converts a collection of values to an R compatible list.

License

Open Source License

Parameter

Parameter Description
list Collection of values

Return

The R representation of the list

Declaration

public static String toDateList(Collection<? extends Date> list) 

Method Source Code


//package com.java2s;
/*/*from  w ww  .  j a v  a  2 s .com*/
* Copyright 2012-2016 Broad Institute, Inc.
* 
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
* 
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
* THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;

public class Main {
    /**
     * Converts a collection of values to an R compatible list. A null list will return NA,
     * otherwise the date will be escaped with single quotes and combined with c().
     * @param list Collection of values
     * @return The R representation of the list
     */
    public static String toDateList(Collection<? extends Date> list) {
        return toDateList(list, "''yyyy-MM-dd''");
    }

    /**
     * Converts a collection of values to an R compatible list formatted by pattern.
     * @param list Collection of values
     * @param pattern format pattern string for each date
     * @return The R representation of the list
     */
    public static String toDateList(Collection<? extends Date> list, String pattern) {

        if (list == null)
            return "NA";
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        StringBuilder sb = new StringBuilder();
        sb.append("c(");
        boolean first = true;
        for (Date date : list) {
            if (!first)
                sb.append(",");
            sb.append(format.format(date));
            first = false;
        }
        sb.append(")");
        return sb.toString();
    }
}

Related

  1. toDate(Object object)
  2. toDate(Object value)
  3. toDateFormat(Date date)
  4. toDateFromLong(Long time)
  5. toDateHeader(long value)
  6. toDateQueryFormat(Date value)
  7. toDateStr(Date d)
  8. ToDateStr(Date dt)
  9. toDateStr(Object o)