get Only Decimal Time - Java java.math

Java examples for java.math:BigDecimal

Description

get Only Decimal Time

Demo Code


//package com.java2s;

import java.math.BigDecimal;

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

public class Main {
    public static void main(String[] argv) {
        Date date = new Date();
        System.out.println(getOnlyDecimalTime(date));
    }//w ww.j a v a 2s .  c o  m

    public static BigDecimal getOnlyDecimalTime(Date date) {
        BigDecimal time = null;

        try {
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);

            String timeStr = cal.get(Calendar.HOUR_OF_DAY)
                    + insertLeftChar(
                            String.valueOf(cal.get(Calendar.MINUTE)), 2,
                            '0');

            time = BigDecimal.valueOf(Long.valueOf(timeStr));
        } catch (Exception e) {
            e.printStackTrace();
        }

        return time;
    }

    public static String insertLeftChar(String source, int length, char ch) {
        StringBuffer temp = new StringBuffer(length);
        if (source.length() <= length) {
            for (int i = 0; i < length - source.length(); i++)
                temp.append(ch);

            temp.append(source);
        }
        return temp.toString();
    }
}

Related Tutorials