Date of Java - to learn how to create and use Date

  Summary 

  Whether you are dealing with the financial transactions or plan the next move, you must know how to build in Java, use and display the date.    This requires you simple access to the API reference to the corresponding categories: a date can create three related targets.    This article tells you, you want to know the contents. 

  Java statistics from January 1, 1970 onwards the number of milliseconds that date.    That is to say, for example, on January 2, 1970, January 1 in the future 86,400,000 milliseconds.    Similarly, on December 31, 1969 in the January 1, 1970 86,400,000 milliseconds.    Date of use of the Java long record of these types of milliseconds. Long as there are unsigned integers, dates in the January 1, 1970, can this after.    Long said the biggest positive types and the biggest negative that can be easily 290,000,000 a year's time, which for most of the time requirements. 

  Date of 

  Date categories can be found in the java.util package, with a long type of values that a specified time.    It's a useful structure function is Date (), it creates a moment that created the object.    GetTime () method returns Date object to the long value.    In the following procedure, I use Date () function to create a structure that is running at the target, and to getTime () method to find the representative of this date ms: 

  Import java.util .*; 

  (Public class Now 
  Public static void main (String [] args) ( 
  Date now = new Date (); 
  Now.getTime long nowLong = (); 
  System.out.println ( "Value is" + nowLong); 
  ) 
  ) 

  When I run this procedure, I have 972568255150. Rapid confirm this figure, at least in a reasonable scope: it less than 31 years, the relative value on January 1, 1970, I wrote this article to the time, is reasonable.    The value of the computer is that millisecond time, people can be reluctant to say "I will see you in 996321998,34." Fortunately, the Java provides a conversion Date object to the string means that a traditional form.    We discussed in the next section DateFormat category, it intuitive to establish date string. 
  DateFormat category 
  DateFormat such a goal is the establishment of a string of people can identify.    However, because of language differences, not all people want to see the same strict format date.    France would prefer to see "25 decembre 2000," but the Americans used to seeing "December 25,2000." So DateFormat example of the creation of a future, and this object contains the date of presentation of information.    If the use of regional computer users default format settings, you may like the following, the creation DateFormat target, the use of getDateInstance () method: 

  DateFormat df = DateFormat.getDateInstance (); 

  DateFormat category java.text package can be found. 

  Convert to a string 

  You can use the format () method for converting Date object to a string.    Below the description of the procedures for examples of this problem: 

  Import java.util .*; 
  Import java.text .*; 

  (Public class NowString 
  Public static void main (String [] args) ( 
  Date now = new Date (); 
  DateFormat df = DateFormat.getDateInstance (); 
  String s = df.format (now); 
  System.out.println ( "Today is the" + s); 
  ) 
  ) 

  In the above code, and has demonstrated no parameters, the default format getDateInstance () method.    Java also provides a few select the date format, you can use the override getDateInstance (int style) was.    For convenience reasons, DateFormat provides several preset constant, you can use these parameters constant.    Below are several SHORT, MEDIUM, LONG, and FULL types of examples: 

  Import java.util .*; 
  Import java.text .*; 

  (Public class StyleDemo 
  Public static void main (String [] args) ( 
  Date now = new Date (); 

  DateFormat df = DateFormat.getDateInstance (); 
  DateFormat df1 = DateFormat.getDateInstance (DateFormat.SHORT); 
  DateFormat df2 = DateFormat.getDateInstance (DateFormat.MEDIUM); 
  DateFormat df3 = DateFormat.getDateInstance (DateFormat.LONG); 
  DateFormat df4 = DateFormat.getDateInstance (DateFormat.FULL); 
  String s = df.format (now); 
  String s1 = df1.format (now); 
  String s2 = df2.format (now); 
  String s3 = df3.format (now); 
  String s4 = df4.format (now); 

  System.out.println ( "(Default) Today is the" + s); 
  System.out.println ( "(SHORT) Today is the" + s1); 
  System.out.println ( "(MEDIUM) Today is the" + s2); 
  System.out.println ( "(LONG) Today is the" + s3); 
  System.out.println ( "(FULL) Today is the" + s4); 
  ) 
  ) 

  Export procedures are as follows: 

  (Default) Today is Nov 8, 2000 
  (SHORT) Today is 11/8/00 
  (MEDIUM) Today is Nov 8, 2000 
  (LONG) Today is November 8, 2000 
  (FULL) Today is Wednesday, November 8, 2000 

  The same procedure, in my computer running on the default settings, change the region is set to Sweden, the output is as follows: 

  (Default) Today is 2000-nov-08 
  (SHORT) Today is 2000-11-08 
  (MEDIUM) Today is 2000-nov-08 
  (LONG) Today is den 8 november 2000 
  (FULL) Today is den 8 november 2000 

  From here, you can see, Sweden is not in the capital (although November or november). Addition, LONG FULL versions of the Swedish and is the same, but American English are different.    Also it is interesting, Swedish word Wednesday, onsdag, is not included in the FULL date, but including English. 
  Note that you can use getDateInstance () method change DateFormat examples of languages; However, in the above example, by changing the control panel Windows98 regional settings do.    Different places at different regions, different results, so good, there were some inadequacies, Java programmers should be aware of these.    One of the advantages is the only Java programmers can write code to display his date, and different regions of the world computer running the same procedure will not date format.    But this is also a drawback, when programmers hope that with a format of the show - and this is also acceptable, for example, in the process of mixing the output text and date, if the text is in English, we hope that the date format is not the other format, such as German or Spanish.    If programmers rely on the date format programming, date format will be the host computer's operating procedures do not have different regional settings. 

  Analysis of string 

  Through parse () method, a string DateFormat able to create a Date object.    This method can be dished out ParseException abnormal, so you must use the appropriate exception handling technology.    The following example procedure string create Date object: 

  Import java.util .*; 
  Import java.text .*; 

  (Public class ParseExample 
  Public static void main (String [] args) ( 
  String ds = "November 1, 2000"; 
  DateFormat df = DateFormat.getDateInstance (); 
  Try
  Date d = df.parse (ds); 
  ) 
  Catch (ParseException e) ( 
  System.out.println ( "Unable to parse" + ds); 
  ) 
  ) 
  ) 

  In the creation of an arbitrary date parse () method useful.    I will adopt another method of creating an arbitrary date.    At the same time, and you will see how the basic date, such as 90 days after the calculation of the other days.    You can use GregorianCalendar category to complete this task. 
  GregorianCalendar category 
  Arbitrary date of the creation of a representative of a way of the use of the constructor function GregorianCalendar it included in the java.util package: 

  GregorianCalendar (int year, int month, int date) 

  Note that the month of January is 0, 1 February is, and so is December 11.    Because most people are accustomed to use words rather than numbers expressed in such procedures may be more legible type Calendar Father expressed in constant use: JANUARY, FEBRUARY, and so on.    Therefore, the creation of Wilbur and Orville force aircraft manufacturers The first date (December 17, 1903), you can use: 

  GregorianCalendar firstFlight = new GregorianCalendar (1903, Calendar.DECEMBER, 17); 

  Clearly out of consideration, you should use in the form of front.    However, you should learn how to read the following short format.    The following examples also said that December 17,1903 (Remember, in the short format, said December 11) 

  GregorianCalendar firstFlight = new GregorianCalendar (1903, 11, 17); 

  In the last one, you learn conversion Date object to a string.    Here, you can do the same thing, but first of all, you need to switch to Date GregorianCalendar object.    To do this, you can use getTime () method, it was inherited from the father of Calendar.    GetTime () method returns the corresponding GregorianCalendar Date object.    You can create GregorianCalendar target Date object to the conversion, and output by the corresponding string of such a process.    Below are examples: 

  Import java.util .*; 
  Import java.text .*; 

  (Public class Flight 

  Public static void main (String [] args) ( 
  GregorianCalendar firstFlight = new GregorianCalendar (1903, Calendar.DECEMBER, 17); 
  Date d = firstFlight.getTime (); 
  DateFormat df = DateFormat.getDateInstance (); 
  String s = df.format (d); 
  System.out.println ( "First flight was" + s); 
  ) 
  ) 

  Sometimes the creation of a representative of the current moment of the GregorianCalendar instance of the class is very useful.    You can not use simple parameters GregorianCalendar constructor function, like this: 

  GregorianCalendar thisday = new GregorianCalendar (); 

  Today, the date of an output example, the use of GregorianCalendar Target: 

  Import java.util .*; 
  Import java.text .*; 

  (Class Today 
  Public static void main (String [] args) ( 
  GregorianCalendar thisday = new GregorianCalendar (); 
  Date d = thisday.getTime (); 
  DateFormat df = DateFormat.getDateInstance (); 
  String s = df.format (d); 
  System.out.println ( "Today is the" + s); 
  ) 
  ) 

  Notes Date () constructor function and GregorianCalendar () constructor function is very similar: both to create an object, a simple conditions, representative of today. 

  Date handling 

  GregorianCalendar to deal with such date.    The method is a useful add (). Use of add () method, you can increase as the years, months, days to Date object.    To use the add () method, you must provide to increase the field, it is necessary to increase the number.    Some useful fields are DATE, MONTH, YEAR, and WEEK_OF_YEAR.    Below the use of the procedures add () method of calculating the next 80 days a date.    Jules in the <80 days Global> is an important figure, the use of this procedure can be calculated Phileas Fogg starting from the day when in October 1872 after 2 to 80 days: 

  Import java.util .*; 
  Import java.text .*; 

  (Public class World 
  Public static void main (String [] args) ( 
  GregorianCalendar worldTour = new GregorianCalendar (1872, Calendar.OCTOBER, 2); 
  WorldTour.add (GregorianCalendar.DATE, 80); 
  Date d = worldTour.getTime (); 
  DateFormat df = DateFormat.getDateInstance (); 
  String s = df.format (d); 
  System.out.println ( "80 day trip will end" + s); 
  ) 
  ) 

  This example is to imagine, but a date increase in the number of days is a general operation: discs can rent three days, the library can borrow books for 21 days, shops often need to buy items sold within 30 days.    Below demonstration of the procedure used in calculation: 

  Import java.util .*; 
  Import java.text .*; 

  (Public class Mortgage 
  Public static void main (String [] args) ( 
  GregorianCalendar mortgage = new GregorianCalendar (1997, Calendar.MAY, 18); 
  Mortgage.add (Calendar.YEAR, 15); 
  Date d = mortgage.getTime (); 
  DateFormat df = DateFormat.getDateInstance (); 
  String s = df.format (d); 
  System.out.println ( "15 year mortgage amortized on" + s);) 
  ) 

  Add () an important side effect is that it changes the original date.    Sometimes, with the original date and the revised date is very important.    Unfortunately, you can not create a simple GregorianCalendar object, and set it equal to the original (equal).    Reason is that the two variables at the same Date () object addresses.    If the Date object to change, the two variables change at the target date.    Instead of this approach, should create a new object.    Below the procedure demonstration of this approach: 

  Import java.util .*; 
  Import java.text .*; 

  (Public class ThreeDates 
  Public static void main (String [] args) ( 
  GregorianCalendar gc1 = new GregorianCalendar (2000, Calendar.JANUARY, 1); 
  GregorianCalendar gc2 = gc1; 
  GregorianCalendar gc3 = new GregorianCalendar (2000, Calendar.JANUARY, 1); 
  / / Three dates all equal to January 1, 2000 

  Gc1.add (Calendar.YEAR, 1); 
  File: / / gc1 and gc2 are changed 

  DateFormat df = DateFormat.getDateInstance (); 

  Date d1 = gc1.getTime (); 
  Date d2 = gc2.getTime (); 
  Date d3 = gc3.getTime (); 

  String s1 = df.format (d1); 
  String s2 = df.format (d2); 
  String s3 = df.format (d3); 

  System.out.println ( "gc1 is" + s1); 
  System.out.println ( "gc2 is" + s2); 
  System.out.println ( "gc3 is" + s3); 
  ) 
  ) 

  Running the program, gc1 and gc2 was a 2001 (because the two objects at the same Date, Date has been changed).    Object gc3 point to a separate Date, it has not been changed. 
  Review date of this section, you will see a basis for real-world examples.    The detailed procedures of the past a specific date.    For example, you read the article, do you want to remember an impressive knowledge point.    If you do not have the same photo memory, you have regular review of these new information, and this will help you remember it.    On the review system, Kurt Hanks and Gerreld L. Pulsipher in their <Five Secrets to Personal Productivity personal capacity five secret> in the discussion, read The first proposal immediately after the recall, then one day, one week , a month after three months later, after one year.    I of this article, you should be immediately recalled that from now retire and play tomorrow, and then one week, one month, three months, 1 year after.    Our procedures will be calculated these dates. 
  This procedure is very useful, it will be a PIM (Personal Information Manager Personal Information Management) are an integral part of the literature and determined time.    In the following proceedings, getDates () method on the date of a return to the array (review date) electronic software useful.    In addition, you can return to a separate date, the use of getFirstDay (), getOneDay (), getOneWeek (), getOnMonth () and getOneYear (). Beyond the scope of this time when the PIM ReviewDates ReviewDates scope of the calculation of time demonstrating how to calculate paragraph.    Now, you can easily modify it to deal with the time you need, like library library, video rental and mortgage calculation.    First, ReviewDates shown in the following categories: 

  Import java.util .*; 
  Import java.text .*; 

  (Public class ReviewDates 
  Private GregorianCalendar firstDay, oneDay, oneWeek, oneMonth, oneQuarter, oneYear; 
  Final int dateArraySize = 6; 

  ReviewDates (GregorianCalendar gcDate) ( 
  Int year = gcDate.get (GregorianCalendar.YEAR); 
  Int month = gcDate.get (GregorianCalendar.MONTH); 
  Int date = gcDate.get (GregorianCalendar.DATE); 

  FirstDay = new GregorianCalendar (year, month, date); 
  OneDay = new GregorianCalendar (year, month, date); 
  OneWeek = new GregorianCalendar (year, month, date); 
  OneMonth = new GregorianCalendar (year, month, date); 
  OneQuarter = new GregorianCalendar (year, month, date); 
  OneYear = new GregorianCalendar (year, month, date); 

  OneDay.add (GregorianCalendar.DATE, 1); 
  OneWeek.add (GregorianCalendar.DATE 7); 
  OneMonth.add (GregorianCalendar.MONTH, 1); 
  OneQuarter.add (GregorianCalendar.MONTH, 3); 
  OneYear.add (GregorianCalendar.YEAR, 1); 
  ) 

  ReviewDates () ( 
  This (new GregorianCalendar ()); 
  ) 

  Public void listDates () ( 
  DateFormat df = DateFormat.getDateInstance (DateFormat.LONG); 
  Date startDate = firstDay.getTime (); 
  Date date1 = oneDay.getTime (); 
  Date date2 = oneWeek.getTime (); 
  Date date3 = oneMonth.getTime (); 
  Date date4 = oneQuarter.getTime (); 
  Date date5 = oneYear.getTime (); 

  String ss = df.format (startDate); 
  String ss1 = df.format (date1); 
  String ss2 = df.format (date2); 
  String ss3 = df.format (date3); 
  String ss4 = df.format (date4); 
  String ss5 = df.format (date5); 

  System.out.println ( "Start date is" + ss); 
  System.out.println ( "Following review dates are:"); 
  System.out.println (ss1); 
  System.out.println (ss2); 
  System.out.println (ss3); 
  System.out.println (ss4); 
  System.out.println (ss5); 
  System.out.println (); 
  ) 

  GetDates public GregorianCalendar [] () ( 
  GregorianCalendar [] = new GregorianCalendar memoryDates [dateArraySize]; 
  MemoryDates [0] = firstDay; 
  MemoryDates [1] = oneDay; 
  MemoryDates [2] = oneWeek; 
  MemoryDates [3] = oneMonth; 
  MemoryDates [4] = oneQuarter; 
  MemoryDates [5] = oneYear; 
  Return memoryDates; 
  ) 

  Public GregorianCalendar getFirstDay () ( 
  Return this. FirstDay; 
  ) 

  Public GregorianCalendar getOneDay () ( 
  Return this. OneDay; 
  ) 

  Public GregorianCalendar getOneWeek () ( 
  Return this. OneWeek; 
  ) 

  Public GregorianCalendar getOneMonth () ( 
  Return this. OneMonth; 
  ) 

  Public GregorianCalendar getOneQuarter () ( 
  Return this. OneQuarter; 
  ) 

  Public GregorianCalendar getOneYear () ( 
  Return this. OneYear; 
  ) 
  ) 

  Below are the categories listed ReviewDates use of the date of review procedures examples: 

  Import java.util .*; 

  (Public class ShowDates 
  Public static void main (String [] args) ( 
  ReviewDates rd = new ReviewDates (); 
  Rd.listDates (); 

  GregorianCalendar gc = new GregorianCalendar (2001, Calendar.JANUARY, 15); 
  ReviewDates jan15 = new ReviewDates (gc); 
  Jan15.listDates (); 
  ) 
  ) 

  Aggregate 

  This article introduces the date on the deal with three important categories: Date, DateFormat, GregorianCalendar. These classes let you create date, converted into string, and date of the basic elements.    Java in dealing with the date of this article is only the tip of the iceberg.    However, I am here on classes and methods not only do you learn a springboard for advanced technology, these classes and methods themselves can usually handle a lot of tasks related to the date 

Bookmark it: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Google
  • DotNetKicks
  • DZone
  • Furl
  • Netvouz

Releated Articles

  • Popuklar Articles

0 Comments to “Date of Java - to learn how to create and use Date”

No Comments. Send your comment.

Leave a Reply

You must be logged in to post a comment.