Java Timestamp Convert To to_timestamp(String date)

Here you can find the source of to_timestamp(String date)

Description

ttimestamp

License

Open Source License

Declaration

public static Timestamp to_timestamp(String date) 

Method Source Code


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

import java.sql.Timestamp;

import java.util.Calendar;
import java.util.Date;

import java.util.StringTokenizer;

public class Main {
    public static Timestamp to_timestamp(String date) {
        return new Timestamp(to_date(date).getTime());
    }/*from  www .  j av a  2 s. co  m*/

    public static Date to_date(String date) {
        Calendar cd = Calendar.getInstance();
        StringTokenizer token = new StringTokenizer(date, "-/ :");
        if (token.hasMoreTokens()) {
            cd.set(Calendar.YEAR, Integer.parseInt(token.nextToken()));
        } else {
            cd.set(Calendar.YEAR, 1970);
        }
        if (token.hasMoreTokens()) {
            cd.set(Calendar.MONTH, Integer.parseInt(token.nextToken()) - 1);
        } else {
            cd.set(Calendar.MONTH, 0);
        }
        if (token.hasMoreTokens()) {
            cd.set(Calendar.DAY_OF_MONTH, Integer.parseInt(token.nextToken()));
        } else {
            cd.set(Calendar.DAY_OF_MONTH, 1);
        }
        if (token.hasMoreTokens()) {
            cd.set(Calendar.HOUR_OF_DAY, Integer.parseInt(token.nextToken()));
        } else {
            cd.set(Calendar.HOUR_OF_DAY, 0);
        }
        if (token.hasMoreTokens()) {
            cd.set(Calendar.MINUTE, Integer.parseInt(token.nextToken()));
        } else {
            cd.set(Calendar.MINUTE, 0);
        }
        if (token.hasMoreTokens()) {
            cd.set(Calendar.SECOND, Integer.parseInt(token.nextToken()));
        } else {
            cd.set(Calendar.SECOND, 0);
        }
        if (token.hasMoreTokens()) {
            cd.set(Calendar.MILLISECOND, Integer.parseInt(token.nextToken()));
        } else {
            cd.set(Calendar.MILLISECOND, 0);
        }
        return cd.getTime();
    }
}

Related

  1. timestampToParts(long time)
  2. timestampToPrettyString(long ts)
  3. timestampToSearchString(final Timestamp timestamp)
  4. timestampToString(long time)
  5. timestampToTicks(int year, int month, int day, int hours, int minutes, int seconds, int milliseconds)
  6. toCalendar(final Timestamp ts)
  7. toCalendar(java.sql.Timestamp stamp)
  8. toCalendar(Timestamp stamp)
  9. toCalendar(Timestamp timestamp)