Java SQL Date Parse StrToDate(String Formate, String date)

Here you can find the source of StrToDate(String Formate, String date)

Description

Str To Date

License

Open Source License

Declaration

public String StrToDate(String Formate, String date) 

Method Source Code


//package com.java2s;
import java.text.*;
import java.util.*;

public class Main {
    public String StrToDate(String Formate, String date) {
        Calendar c = Calendar.getInstance();
        try {/*w w w.j av  a  2 s  .  c om*/
            c.set(Calendar.YEAR, Integer.parseInt(date.substring(0, 4)));
            c.set(Calendar.MONTH, Integer.parseInt(date.substring(5, 7)) - 1);
            c.set(Calendar.DATE, Integer.parseInt(date.substring(8, 10)));
            java.util.Date d = c.getTime();
            SimpleDateFormat f = new SimpleDateFormat(Formate);
            return f.format(d);
        } catch (Exception e) {
            System.err.println(e + date);
            return "";
        }
    }

    public static String StrToDate(String date) {
        Calendar c = Calendar.getInstance();
        try {
            c.set(Calendar.YEAR, Integer.parseInt(date.substring(0, 4)));
            c.set(Calendar.MONTH, Integer.parseInt(date.substring(5, 7)) - 1);
            c.set(Calendar.DATE, Integer.parseInt(date.substring(8, 10)));
            java.util.Date d = c.getTime();
            SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
            return f.format(d);
        } catch (Exception e) {
            System.err.println(e + date);
            return "";
        }
    }

    public static Date strToDate(String str) {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR, Integer.parseInt(str.substring(0, 4)));
        c.set(Calendar.MONTH, Integer.parseInt(str.substring(5, 7)) - 1);
        c.set(Calendar.DATE, Integer.parseInt(str.substring(8, 10)));
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);

        return c.getTime();

    }

    public String getTime() {
        java.sql.Time ent = new java.sql.Time(System.currentTimeMillis());
        return ent.toString();
    }
}

Related

  1. stringToSqlDate(String data)
  2. stringToSqlDate(String date)
  3. stringToSqlDate(String dateVal)
  4. stringToSQLDate(String pstrValue, String pstrDateFormat)
  5. stringToSqlDate(String str)
  6. strToDate(String str)
  7. strToDate(String strDate, String formator)
  8. strToDate(String stringDate)
  9. StrToDate(String val)