Java之间String转换为time的方法

第一种:

package org.kodejava.example.util;

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

public class StringToTimeExample {
    public static void main(String[] args) {        
        //
        // A string of time information
        //
        String time = "15:30:18";

        //
        // Create an instance of SimpleDateFormat with the specified
        // format.
        //
        DateFormat sdf = new SimpleDateFormat("hh:mm:ss");
        try {
            // 
            // The get the date object from the string just called the 
            // parse method and pass the time string to it. The method 
            // throws ParseException if the time string is in an 
            // invalid format. But remember as we don't pass the date 
            // information this date object will represent the 1st of
            // january 1970.
            Date date = sdf.parse(time);            
            System.out.println("Date and Time: " + date);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


第二种:

public static Time parseHHMM(String time)
{
Time t = new Time();
// if the format is not "HH:MM" we return null
try
{
String hours = time.substring(0, 2);
String minutes = time.substring(3);

t.hour = Integer.parseInt(hours);
t.minute = Integer.parseInt(minutes);
}
catch (Exception e) // TODO is this nice enough?
{
return null;
}

return t;
}


转载于:https://my.oschina.net/haquanwen/blog/56185

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值