get time as HH:mm - Android java.util

Android examples for java.util:Time

Description

get time as HH:mm

Demo Code


//package com.java2s;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Main {
    public static String getHHmm(long unixLong) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(unixLong);//ww w. j  ava  2  s. c  om
        SimpleDateFormat dateformat = new SimpleDateFormat("HH:mm");
        return dateformat.format(c.getTime());
    }

    public static Date format(String s, String forma) {
        Date lastdt = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(forma);
            lastdt = sdf.parse(s);
            return lastdt;
        } catch (Exception e) {
            return new Date();
        }
    }

    public static Date format(String s) {
        Date lastdt = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            lastdt = sdf.parse(s);
            return lastdt;
        } catch (Exception e) {
            return new Date();
        }
    }

    public static Date format(Date s, String forma) {
        Date lastdt = null;
        String date = getDateTime(s, forma);
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(forma);
            lastdt = sdf.parse(date);
            return lastdt;
        } catch (Exception e) {
            System.out.println("s");
            return new Date();
        }
    }

    public static String getDateTime(Date sDate, String forma) {
        if (sDate == null)
            return "";
        try {
            return new SimpleDateFormat(forma).format(sDate);
        } catch (Exception ex) {
            return "";
        }
    }

    public static String getDateTime(Date sDate) {
        if (sDate == null)
            return "";
        try {
            return new SimpleDateFormat("yyyy-MM-dd").format(sDate);
        } catch (Exception ex) {
            return "";
        }
    }
}

Related Tutorials