欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java实现时间与字符串之间转换

程序员文章站 2023-12-02 20:07:10
本文实例为大家分享了java实现时间与字符串之间转换的具体代码,供大家参考,具体内容如下 1. long字符串转换成yyyy-mm-dd hh:mm:ss格式输出...

本文实例为大家分享了java实现时间与字符串之间转换的具体代码,供大家参考,具体内容如下

1. long字符串转换成yyyy-mm-dd hh:mm:ss格式输出

import java.text.simpledateformat; 
import java.util.date; 
//将long字符串转换成格式时间输出 
public class longtostring { 
public static void main(string argsp[]){ 
  string time="1256006105375"; 
 
  date date=new date(long.parselong(time)); 
  simpledateformat formatter=new simpledateformat("yyyy-mm-dd hh:mm:ss"); 
  time=formatter.format(date); 
  system.out.println(time); 
} 
} 

2. 字符串转换成时间

import java.text.simpledateformat; 
import java.util.date; 
 
import ognl.parseexception; 
 
public class stringtodate { 
public static void main(string argsp[]) throws exception{ 
  string time="2010-11-20 11:10:10"; 
 
  date date=null; 
  simpledateformat formatter=new simpledateformat("yyyy-mm-dd hh:mm:ss"); 
  date=formatter.parse(time); 
  system.out.println(date); 
} 
} 

3. 取得当前系统时间,返回yyyy-mm-dd hh:mm:ss字符串

import java.text.simpledateformat; 
import java.util.date; 
 
public class stringtodate { 
public static void main(string argsp[]) throws exception{ 
   
  date date=new date(); 
  simpledateformat formatter=new simpledateformat("yyyy-mm-dd hh:mm:ss"); 
  string time=formatter.format(date); 
  system.out.println(time); 
} 
} 

4. 取得当前系统时间,返回 hh:mm:ss字符串

import java.text.simpledateformat; 
import java.util.date; 
 
public class stringtodate { 
public static void main(string argsp[]) throws exception{ 
   
  date date=new date(); 
  simpledateformat formatter=new simpledateformat("hh:mm:ss"); 
  string time=formatter.format(date); 
  system.out.println(time); 
} 
} 

5.将20101125102503转换成2010-11-25 10:25:03输出

import java.text.simpledateformat; 
import java.util.date; 
 
public class stringtodate { 
public static void main(string argsp[]) throws exception{ 
   
  string time="20101125102503"; 
  simpledateformat formatter1=new simpledateformat("yyyy-hh-dd hh:mm:ss"); 
  simpledateformat formatter2=new simpledateformat("yyyyhhddhhmmss"); 
  time=formatter1.format(formatter2.parse(time)); 
  system.out.println(time); 
} 
} 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。