In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail how to convert the Long type into the String type in java. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
Java Long type changes to String type 1, Long.ValueOf ("String") returns Long wrapper type data
Packing type: Byte,Integer,Short,Long,Boolean,Character,Float,Double, etc.
2. Long.parseLong ("String") returns the long basic data type
Basic data types: byte,int,short,long,boolean,char,float,double, etc.
Note:
1. The string cannot contain characters other than numbers, otherwise an error will be reported, java.lang.NumberFormatException
2. The length of the string should be limited, otherwise an error will be reported.
For example: String s = "1234567899876543210000", if it exceeds 19 digits, it will report an error, java.lang.NumberFormatException.
String s = "12345698798765432100". If you don't exceed it, you won't make a mistake.
The reason is that the maximum bit of Long type Long.MAX_VALUE = 9223372036854775807, which is greater than the error, and the minimum bit Long.MIN_VALUE =-9223372036854775808. If it is less than this value, an error will be reported.
Conversion between Long, String and Date types in Java 1. Convert Java.util.Date type to long type Date date=new Date (); System.out.println (date.getTime ())
Parsing: where getTime () returns as long, with a length of 13, indicating milliseconds; if you want to get the number of seconds, you only need to divide by 1000.
Long mseconds=date.getTime () / 1000Trac2, long type converted to java.util.Date type SimpleDateFormat sdf= new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss"); java.util.Date date = new Date (mseconds * 1000); String str = sdf.format (date); System.out.println (str)
The previous mseconds represents the number of seconds, so you need to multiply 1000 to get milliseconds, and then convert it to java.util.Date, which completes the conversion from long to Date; to format the output Date, you can call the format method of SimpleDateFormat to format the output Date.
3. Convert formatted string types such as "2015-08-31 21:08:06" to java.util.Date type String str= "2015-08-31 21:08:06"; SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss"); Date date = (Date) sdf.parse (str); System.out.println (date.getTime ())
The output is as follows:
1441026486000
Sometimes you need to convert the CST time, for example, Wed Sep 16 11:26:23 CST 2009, which can also be solved with SimpleDateFormat's parse.
String str= "Wed Sep 16 11:26:23 CST 2009"; SimpleDateFormat sdf = new SimpleDateFormat ("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US); Date date = (Date) sdf.parse (str)
Remarks (util class):
Import java.text.*;import java.util.*;public class DateFormat {/ * date type formatted output * @ param date * @ return * / public static String dateFormat (Date date) {SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss"); String dateString = formatter.format (date); return dateString } / * * convert "2017-08-9 21:08:06" string to Date * @ param str * @ return * @ throws ParseException * / public static Date StringToDate (String str) throws ParseException {SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss"); Date date = (Date) formatter.parse (str); return date } / * format CST time type string output * @ param str * @ return * @ throws ParseException * / public static String CSTFormat (String str) throws ParseException {SimpleDateFormat formatter = new SimpleDateFormat ("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US); Date date = (Date) formatter.parse (str); return dateFormat (date) } / * convert long type to Date * @ param str * @ return * @ throws ParseException * / public static Date LongToDare (long str) throws ParseException {return new Date (str * 1000) }} this is the end of the article on "how to convert Long types into String types in java". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.