Java SQL Date getNow()

Here you can find the source of getNow()

Description

Get current date in sql format yyyy-MM-dd

License

Apache License

Declaration

public static java.sql.Date getNow() 

Method Source Code

//package com.java2s;
/*/*  w ww  .j  a  v a 2s. c o m*/
 *    Copyright 2003, 2004, 2005, 2006 Research Triangle Institute
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 */

import java.util.Calendar;

import java.util.Date;

import java.util.TimeZone;

public class Main {
    /**
     * Get current date in sql format yyyy-MM-dd
     * @return
     */
    public static java.sql.Date getNow() {
        // set today's date
        Calendar cal = Calendar.getInstance(TimeZone.getDefault());
        String DATE_FORMAT = "yyyy-MM-dd";
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(DATE_FORMAT);
        sdf.setTimeZone(TimeZone.getDefault());
        String now = sdf.format(cal.getTime());
        return java.sql.Date.valueOf(now);
    }

    public static String getTime() {
        Calendar cal = Calendar.getInstance(TimeZone.getDefault());
        String DATE_FORMAT = "HH:mm:ss";
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(DATE_FORMAT);
        sdf.setTimeZone(TimeZone.getDefault());
        String now = sdf.format(cal.getTime());
        return now;
    }

    public static String getTime(Date date) {
        // Calendar cal = Calendar.getInstance(TimeZone.getDefault());
        String DATE_FORMAT = "HH:mm:ss";
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(DATE_FORMAT);
        // sdf.setTimeZone(TimeZone.getDefault());
        String formattedTime = sdf.format(date);
        return formattedTime;
    }
}

Related

  1. getLastDay()
  2. getMonthInt(String month)
  3. getNextDayStr(String curday)
  4. getNextDayStr(String curday)
  5. getNow()
  6. getNowHour()
  7. getProtobufClass(Object value, Class protobufClass)
  8. getTodayAndTomorrow()
  9. getWeekNumber()