Date Format SIMPLEDATEFORMAT

zhaozj2021-02-16  78

Write a class in the afternoon, format the date-name SimpleDateFormat. Let's take a look, exchange. Healing: A class simpleDateFormat in Java, when constructing an object, the way to display the display, you can get this format of the current date through a method of this class. Think about the function in Flash, there is no such function. Every time I have to get the date, I want to call the N methods in Date. The last string is pigureder to get the desired format, I feel trouble, start my brain, write this class.

Use: Write VAR SIM = New SimpleDateFormat ("YYYY / MM / DD") in Flash; then a date object var Date = new date (); then format the DATE, call the method and display: TRACE (Sim.Format (Date) This can get the date of such YYYY / MM / DD 2004/07/03, if you write VAR SIM = New SimpleDateFormat when constructing the SIM object ("YY / MM / DD "); you can get the date of such YY / mm / DD 04/07/03, as long as you want what format, just write his format when constructing the SIM object. That is, the date of separation, and display methods can be arbitrarily specified by you. Here, y said YY, YY indicates that 2 digits, yyyy shows all, m indicates the month, you must use the MMD to represent the day, if you don't want the day, you can not Write it, for example, don't have years, only the month can be like this to be like SimpleDateFormat ("mm / dd");

The specific code is as follows, everyone discusses, what is wrong, I will correct it again, write a detailed tutorial again, and provide this class to everyone, a class we develop, I use it, huh, huh. I will also write some kinds of classes, I hope that my friends who are interested will pay attention.

/ *** Title: Date Class Format * Description: * Company: * Copyright * @author wei386 * @version 1.0 * / class SimpleDateFormat {private var timetype: String; private var date: Date; private var year: String; private var month: string; private var day: string; function SimpleDateFormat (type: string) {this.timetype = type;} / ** * obtained containing start position of the string firstString * @param string firststring * @return Number * / private function firstString (firststring: string): Number {var starts: Number = timetype.indexOf (firststring); return starts;} / ** * get a string containing the end position of firstString * @param string firststring * @return Number * / private function lastString (laststring: String): Number {var ends: Number = timetype.lastIndexOf (laststring); return ends;} / ** * look for years * @param * @return Boolean * / private function findYear (): Boolean { Var starts = this.firststring ("y"); var ends = this.laststring ("y"); var yeaartype = ends-starts; if (Yeartype == 3) {this.year = " this.date. GetFullyear (); return true;} else f (Yeartype == 1) {var sho RTYEAR: STRING = "" this.date.GetFullyEar (); this.year = shortyear.substring (2, 4); return true} else if (starts == - 1 || Ends == - 1) {Return False } Else {trace ("The year format is incorrect"); return false;}} / ** * Find Whether the month * @ @return boolean * / private function FindMonth (): boolean {var starts = this.firststring "M"); var ends = this.laststring ("m"); var monthtype = ends-starts; if (starts == - 1) {Return False;} else f (Monthtype == 1) {var newmonth = this .date.getMonth () 1; if (newMonth <10) {this.month = "0"

newmonth;} else {this.month = " newmonth;} return true} else {trace (" The month format is incorrect "); Return False;}} / ** * Find no day * @param * @return boolean * / private function FINDDAY (): Boolean {var starts = this.firststring ("d"); var ends = this.laststring ("d"); var monthtype = ends-starts; if (starts == - 1) { Return False;} else == 1) {var newday = this.date.getdate (); if (newDay <10) {this.day = "0" newday;} else {this.day = " new true} else {trace ("The day format is incorrect"); Return False;}} / ** * Formatted in the format in the format, format the bi-passing Date object * @Param Date pdate * @return String * / public function format (pdate: Date): String {this.date = pdate; var stringLength = Timetype.Length; var ystarts = this.firststring ("y"); var Yends = this.laststring ("y") Var mstarts = this.firststring ("m"); var mends = this.laststring ("m"); var DStarts = this.firststring ("d"); var dends = this.laststring ("d"); VAR Isyear = this.findyear (); var ismonth = this.findmonth (); Var isday (); if (isyear) {this.ShowResult (YStarts, Yends, this.Year);} if (ismonth) {this.showResult (mstarts, mends, this.month);} if (isday ) {This.ShowResult (DStarts, dends, this.day;} Return Timetype;} / ** * replaced the date result in the corresponding location * @Param Number NStart, Number Nend, String Ndate * @return void * / private Function ShowResult (NStart: Number, Nend: Number, NDATE: STRING): Void {this.timeType = this.timetype.substring (0, nStart) NDATE THIS.TIMEPE.SUBSTRING (Nend

转载请注明原文地址:https://www.9cbs.com/read-13561.html

New Post(0)