Java Date Now getCurrentTime(String format)

Here you can find the source of getCurrentTime(String format)

Description

get Current Time

License

Open Source License

Declaration

public static String getCurrentTime(String format) 

Method Source Code

//package com.java2s;
/*/* w  w w .ja v a 2s.  c  o  m*/
Copyright (C) 2011  Diego Darriba, David Posada
    
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
    
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Locale;

public class Main {
    public static String getCurrentTime(String format) {
        Calendar cal = new GregorianCalendar();
        SimpleDateFormat date_format = new SimpleDateFormat(format);
        return date_format.format(cal.getTime());
    }

    public static String format(double number, int totalLength, int decimalPlaces, boolean exp) {
        StringBuffer sb;
        String format;
        if (exp) {
            format = "%" + totalLength + "." + decimalPlaces + "e";
        } else {
            format = "%" + totalLength + "." + decimalPlaces + "f";
        }
        sb = new StringBuffer(String.format(Locale.ENGLISH, format, number));
        // normalize string to size 6
        for (int i = sb.length(); i < totalLength; i++) {
            sb.insert(0, " ");
        }

        return sb.toString();
    }
}

Related

  1. getCurrentTime(long delay, long interval)
  2. getCurrentTime(String format)
  3. getCurrentTime(String format)
  4. getCurrentTime(String format)
  5. getCurrentTime(String format)
  6. getCurrentTime(String formatter)
  7. getCurrentTime(String pattern)
  8. getCurrentTime(String pattern)
  9. getCurrentTime(String pattern)