集册 Java实例教程 分析时间戳以获取其时间部分

分析时间戳以获取其时间部分

欢马劈雪     最近更新时间:2020-01-02 10:19:05

389
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
分析时间戳以获取其时间部分

import java.text.ParsePosition;

import java.text.SimpleDateFormat;
/**
来 自 时 代 J a v a 公 众 号 - N o w J a v  a . c o m
**/

import java.util.Calendar;

import java.util.Date;


public class Main {

  public static void main(String[] args){

    String input = "2013-04-03 09:10:40.325";


    // Prepare the pattern

    String pattern = "yyyy-MM-dd HH:mm:ss.SSS" ;


    SimpleDateFormat sdf = new SimpleDateFormat(pattern);


    // Parse the text into a Date object

    Date dt = sdf.parse(input, new ParsePosition(0));

    System.out.println(dt);

    

    // Get the Calendar instance

    Calendar cal = Calendar.getInstance();

      //N o w J a v a . c o m 提供

    // Set the time

    cal.setTime(dt);


    // Print time parts

    System.out.println("Hour:" + cal.get(Calendar.HOUR));

    System.out.println(
展开阅读全文