Java Date from String convertToDate(final String str, final DateFormat dateFormat)

Here you can find the source of convertToDate(final String str, final DateFormat dateFormat)

Description

convert To Date

License

Open Source License

Declaration

public static Date convertToDate(final String str, final DateFormat dateFormat) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
    public static Date convertToDate(final long m) {
        return new Date(m);
    }/*from ww  w. j  a  va2  s. c o m*/

    public static Date convertToDate(final byte[] bytes) {
        return convertToDate(convertToLong(bytes));
    }

    public static Date convertToDate(final String str, final DateFormat dateFormat) {
        try {
            return dateFormat.parse(str);
        } catch (ParseException e) {
            return null;
        }
    }

    public static Date convertToDate(final String str, final String format) {
        return convertToDate(str, new SimpleDateFormat(format));
    }

    public static Date convertToDate(final String str) {
        return convertToDate(str, new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"));
    }

    public static long convertToLong(final byte[] bytes) {
        return (0xffL & (long) bytes[0]) | (0xff00L & ((long) bytes[1] << 8))
                | (0xff0000L & ((long) bytes[2] << 16)) | (0xff000000L & ((long) bytes[3] << 24))
                | (0xff00000000L & ((long) bytes[4] << 32)) | (0xff0000000000L & ((long) bytes[5] << 40))
                | (0xff000000000000L & ((long) bytes[6] << 48)) | (0xff00000000000000L & ((long) bytes[7] << 56));
    }

    public static long convertToLong(final String str, final int radix) {
        return Long.parseLong(str, radix);
    }

    public static long convertToLong(final String str) {
        return Long.parseLong(str);
    }
}

Related

  1. convertToDate(String date)
  2. convertToDate(String dateAsString)
  3. convertToDate(String dateString)
  4. convertToDate(String dateString, String format)