Android JAVA string to date or date to string, androidjava

Source: Internet
Author: User

Android JAVA string to date or date to string (to), androidjava

Usage: SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss ");

This line is the most important, it establishes the conversion format, yyyy is the complete year of the AD, MM is the month, dd is the date, as for HH: mm: ss

I don't need to explain it again!

PS: Why are some formats in upper case and some in lower case to avoid confusion? For example, MM is a month, mm is a minute, and HH is 24 small

Time, while hh is 12-Hour.

1. string to date
19:20:00 to convert it into a Date, you can use date = sdf. parse ("19:20:00 ");

2. Convert date to string

If you convert today's Date to a String, you can use String str = sdf. format (new Date ());

The format of this string is similar to 19:20:00.

Through this API, we can convert the date to the string format we want, for example, to output the date to 2008

In May July 10, we can write as follows:

SimpleDateFormat sdf = new SimpleDateFormat ("MM dd, yyyy ");

String str = sdf. format (new Date ());

Str will be output in the format we set.

A simple example written in the appendix:

1 import java. util. date; 2 import java. text. parseException; 3 import java. text. simpleDateFormat; 4 5 public class ConvertDemo {6 7/** 8 * date converted to String 9 * @ param Date 10 * @ return str 11 */12 public static String DateToStr (date Date) {13 14 SimpleDateFormat format = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss"); 15 String str = format. format (date); 16 return str; 17} 18 19/** 20 * String converted to date 21 * @ param str 22 * @ return Date 23 */24 public static date StrToDate (String str) {25 26 SimpleDateFormat format = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss"); 27 Date date = null; 28 try {29 date = format. parse (str); 30} catch (ParseException e) {31 e. printStackTrace (); 32} 33 return date; 34} 35 36 public static void main (String [] args) {37 38 Date date = new Date (); 39 System. out. println ("date to string:" + ConvertDemo. dateToStr (date); 40 System. out. println ("string to date:" + ConvertDemo. strToDate (ConvertDemo. dateToStr (date); 41 42} 43 44} 45 46 date Format Conversion in Java 47 48/** 49 * String Conversion to java. util. date <br> 50 * supported format: yyyy. MM. dd G 'at 'hh: mm: ss z like '2017-1-1 AD at 22:10:59 PSD '<br> 51 * yy/MM/dd hh: mm: ss such as '2014/1 17:55:00 '<br> 52 * yy/MM/dd HH: mm: ss pm such as '2014/1 17:55:00 pm '<br> 53 * yy-MM-dd HH: mm: ss, for example, '2017-1-1 17:55:00 '<br> 54 * @ param time String <br> 55 * @ return Date <br> 56 */57 public static Date stringToDate (String time) {58 SimpleDateFormat formatter; 59 int tempPos = time. indexOf ("AD"); 60 time = time. trim (); 61 formatter = new SimpleDateFormat ("yyyy. MM. dd G 'at' hh: mm: ss z "); 62 if (tempPos>-1) {63 time = time. substring (0, tempPos) + 64 "ad" + time. substring (tempPos + "AD ". length (); // china 65 formatter = new SimpleDateFormat ("yyyy. MM. dd G 'at' hh: mm: ss z "); 66} 67 tempPos = time. indexOf ("-"); 68 if (tempPos>-1 & (time. indexOf ("") <0) {69 formatter = new SimpleDateFormat ("yyyyMMddHHmmssZ"); 70} 71 else if (time. indexOf ("/")>-1) & (time. indexOf ("")>-1) {72 formatter = new SimpleDateFormat ("yyyy/MM/dd HH: mm: ss"); 73} 74 else if (time. indexOf ("-")>-1) & (time. indexOf ("")>-1) {75 formatter = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss"); 76} 77 else if (time. indexOf ("/")>-1) & (time. indexOf ("am")>-1) | (time. indexOf ("pm")>-1) {78 formatter = new SimpleDateFormat ("yyyy-MM-dd KK: mm: ss "); 79} 80 else if (time. indexOf ("-")>-1) & (time. indexOf ("am")>-1) | (time. indexOf ("pm")>-1) {81 formatter = new SimpleDateFormat ("yyyy-MM-dd KK: mm: ss "); 82} 83 ParsePosition pos = new ParsePosition (0); 84 java. util. date ctime = formatter. parse (time, pos); 85 86 return ctime; 87} 88 89/** 90 * Add java. util. convert the Date format to the string format 'yyyy-MM-dd HH: mm: ss' (in 24-hour format) <br> 91 * For example, Sat May 11 17:24:21 CST 2002 to '2017-05-11 17:24:21 '<br> 92 * @ param time Date <br> 93 * @ return String <br> 94 */95 96 97 public static String dateToString (Date time) {98 SimpleDateFormat formatter; 99 formatter = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss"); 100 String ctime = formatter. format (time); 101 102 return ctime; 103} 104 105 106/** 107. util. convert the Date format to the string format 'yyyy-MM-dd HH: mm: ss A' (in 12-hour format) <br> 108 * For example, Sat May 11 17:23:22 CST 2002 to '2017-05-11 05:23:22 PM '<br> 2002 * @ param time Date <br> 109 * @ param x any integer of int, for example: 1 <br> 111 * @ return String <br> 112 */113 public static String dateToString (Date time, int x) {114 SimpleDateFormat formatter; 115 formatter = new SimpleDateFormat ("yyyy-MM-dd KK: mm: ss a"); 116 String ctime = formatter. format (time); 117 118 return ctime; 119} 120 121 122 123/** 124 * obtain the current system time: the returned value is in the following format: 125 *-10-30 20: 24: 39126 * @ return String127 */128 public static String Now () {129 return dateToString (new Date (); 130} 131 132/** 133 * Get the current system time: the return value is in the following format: 134*135-10-30 08:28:56 PM 136 * @ param hour is an integer of 138 * @ return stringpoints */public static String Now (int hour) {139 return dateToString (new Date (), hour); 140} 141 142 143/** 144 * Get the current system time: the return value is 145*2002-10-30146 * @ return String147 */148 public static String getYYYY_MM_DD () {149 return dateToString (new Date ()). substring (150); 151 152} 153 154 155/** * obtain the given system time: the return value is 156*2002-10-30157 * @ return String158 */159 public static String getYYYY_MM_DD (String date) {160 return date. substring (161); 162 163} 164 165 166 public static String getHour () {167 SimpleDateFormat formatter; 168 formatter = new SimpleDateFormat ("H"); String ctime = formatter. format (new Date (); 169 return ctime; 170} 171 172 public static String getDay () {173 SimpleDateFormat formatter; 174 formatter = new SimpleDateFormat ("d "); 175 String ctime = formatter. format (new Date (); 176 return ctime; 177} 178 179 public static String getMonth () {180 SimpleDateFormat formatter; 181 formatter = new SimpleDateFormat ("M "); 182 String ctime = formatter. format (new Date (); 183 return ctime; 184} 185 186 public static String getYear () {187 SimpleDateFormat formatter; 188 formatter = new SimpleDateFormat ("yyyy "); 189 String ctime = formatter. format (new Date (); 190 return ctime; 191} 192 193 public static String getWeek () {194 SimpleDateFormat formatter; 195 formatter = new SimpleDateFormat ("E "); 196 String ctime = formatter. format (new Date (); 197 return ctime; 198}

Date comparison:

In JAVA, Date and DateFormat can be used for Date calculation and comparison. The following is an example code:

1 import java. text. *; 2 import java. util. *; 3 4 public class Test {5 6 public static void main (String [] args) {7 try {8 Date date = new Date (); 9 DateFormat df = DateFormat. getDateTimeInstance (); 10 String now = df. format (date); 11 System. out. println ("current time:" + now); 12 13 System. out. println ("whether the current time is before:" + date. before (df. parse ("16:00:00"); 14} 15 catch (ParseException e) {System. out. print (e. getMessage (); 16} 17} 18}

 

View more of the following original urls:

(

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.