Java Timestamp Create toTimestamp(String yyyymmddhhmmss)

Here you can find the source of toTimestamp(String yyyymmddhhmmss)

Description

to Timestamp

License

Open Source License

Parameter

Parameter Description
yyyymmddhhmmss ex. "20070101120500" , "20530101120500"

Return

Timestamp

Declaration

public static Timestamp toTimestamp(String yyyymmddhhmmss) 

Method Source Code


//package com.java2s;
/*//from  www  .ja  v a 2  s  .  c om
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import java.sql.Timestamp;

public class Main {
    /**
     * @param yyyymmddhhmmss ex. "20070101120500" , "20530101120500"
     * @return Timestamp
     */
    public static Timestamp toTimestamp(String yyyymmddhhmmss) {
        if (yyyymmddhhmmss == null) {
            return null;
        }
        String year = yyyymmddhhmmss.substring(0, 4);
        if (Integer.valueOf(year) > 2400) {
            year = String.valueOf(Integer.parseInt(year) - 543);
        }
        String month = yyyymmddhhmmss.substring(4, 6);
        String day = yyyymmddhhmmss.substring(6, 8);
        String hour = "";
        String minute = "";
        String second = "";

        if (yyyymmddhhmmss.length() == 8) {
            hour = "00";
            minute = "00";
            second = "00";
        } else {
            hour = yyyymmddhhmmss.substring(8, 10);
            minute = yyyymmddhhmmss.substring(10, 12);
            second = yyyymmddhhmmss.substring(12, 14);
        }
        return Timestamp.valueOf(year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second + ".0");
    }
}

Related

  1. toTimestamp(String dt)
  2. toTimestamp(String s)
  3. toTimeStamp(String sDate, String format)
  4. toTimestamp(String str, String format)
  5. toTimestamp(String value)
  6. toTimestamp2(long seconds, int fraction, int width)
  7. toTimestamp2(long seconds, int nanos)
  8. toTimestamp2(long value, int nanos, int meta)
  9. toTimestampByHour(Date date, int hour)