この例では、Javaの特定のコードを共有して、リファレンスの時間と文字列の間の変換を実装しています。特定のコンテンツは次のとおりです
1.長い文字列をyyyy-mm-dd hh:mm:ss形式出力に変換します
java.text.simpledateformatをインポートします。 Import Java.util.date; //長い文字列をフォーマットに変換する時間出力パブリッククラスlongtostring {public static void main(string argsp []){string time = "1256006105375";日付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。文字列を時間に変換します
java.text.simpledateformatをインポートします。 Import Java.util.date; ognl.parseexceptionをインポートします。 public class stringtodate {public static void main(string argsp [])throws exception {string time = "2010-11-20 11:10:10";日付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 stringを返します
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文字列を返します
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出力に変換します
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( "yyyhhddhhmmss"); time = formatter1.format(formatter2.parse(time)); System.out.println(time); }}上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。