Iso Date : Date « Date Type « Android






Iso Date

     
/*******************************************************************************
 * Copyright (c) 2011 Benjamin Gmeiner.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Benjamin Gmeiner - Project Owner
 ******************************************************************************/

//package com.mediaportal.ampdroid.utils;

import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class IsoDate {

  public static final int DATE = 1;
  public static final int TIME = 2;
  public static final int DATE_TIME = 3;

  public IsoDate() {
  }

  static void dd(StringBuffer stringbuffer, int i) {
    stringbuffer.append((char) (48 + i / 10));
    stringbuffer.append((char) (48 + i % 10));
  }

  public static String dateToString(Date date, int i) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeZone(TimeZone.getTimeZone("GMT"));
    calendar.setTime(date);
    StringBuffer stringbuffer = new StringBuffer();
    if ((i & 1) != 0) {
      int j = calendar.get(1);
      dd(stringbuffer, j / 100);
      dd(stringbuffer, j % 100);
      stringbuffer.append('-');
      dd(stringbuffer, (calendar.get(2) - 0) + 1);
      stringbuffer.append('-');
      dd(stringbuffer, calendar.get(5));
      if (i == 3) {
        stringbuffer.append("T");
      }
    }
    if ((i & 2) != 0) {
      dd(stringbuffer, calendar.get(11));
      stringbuffer.append(':');
      dd(stringbuffer, calendar.get(12));
      stringbuffer.append(':');
      dd(stringbuffer, calendar.get(13));
      stringbuffer.append('.');
      int k = calendar.get(14);
      stringbuffer.append((char) (48 + k / 100));
      dd(stringbuffer, k % 100);
      stringbuffer.append('Z');
    }
    return stringbuffer.toString();
  }

  public static Date stringToDate(String s, int i) {
    Calendar calendar = Calendar.getInstance();
    if ((i & 1) != 0) {
      calendar.set(1, Integer.parseInt(s.substring(0, 4)));
      calendar.set(2, (Integer.parseInt(s.substring(5, 7)) - 1) + 0);
      calendar.set(5, Integer.parseInt(s.substring(8, 10)));
      if (i != 3 || s.length() < 11) {
        calendar.set(11, 0);
        calendar.set(12, 0);
        calendar.set(13, 0);
        calendar.set(14, 0);
        return calendar.getTime();
      }
      s = s.substring(11);
    } else {
      calendar.setTime(new Date(0L));
    }
    calendar.set(11, Integer.parseInt(s.substring(0, 2)));
    calendar.set(12, Integer.parseInt(s.substring(3, 5)));
    calendar.set(13, Integer.parseInt(s.substring(6, 8)));
    int j = 8;
    if (j < s.length() && s.charAt(j) == '.') {
      int k = 0;
      int l = 100;
      do {
        char c = s.charAt(++j);
        if (c < '0' || c > '9') {
          break;
        }
        k += (c - 48) * l;
        l /= 10;
      } while (true);
      calendar.set(14, k);
    } else {
      calendar.set(14, 0);
    }
    if (j < s.length()) {
      if (s.charAt(j) == '+' || s.charAt(j) == '-') {
        calendar.setTimeZone(TimeZone.getTimeZone("GMT"
            + s.substring(j)));
      } else if (s.charAt(j) == 'Z') {
        calendar.setTimeZone(TimeZone.getTimeZone("GMT"));
      } else {
        throw new RuntimeException("illegal time format!");
      }
    }
    return calendar.getTime();
  }
}

   
    
    
    
    
  








Related examples in the same category

1.Write Date to AudioTrack
2.Represents a date using an integer, in a similar fashion to the implementation in Microsoft Excel.
3.A formatter that formats dates to show the elapsed time relative to some base date.
4.Get the string of the date using format "yyyy-MM-dd HH:mm:ss"
5.Parse a string that contains date, return a date object using format "yyyy-MM-dd HH:mm:ss"
6.Create Date from timestamp
7.Convert date in RFC2822 and UTC strings, and to build Date from string of dates in RFC2822 and UTC format
8.Gets a "HH:mm:ss.SSS EEE dd MMM yyyy" representation of a Date
9.Gets a "yyyy MMM dd, HH:mm:ss.SSS" representation of a Date
10.Gets a "EEE, dd MMM yyyy hh:mm:ss 'GMT'" representation of a Date
11.Parses a String for a "EEE, dd MMM yyyy hh:mm:ss 'GMT'" formatted Date
12.Formatting and parsing the various date formats we expect to encounter.
13.ISO8601, ISO8601, RFC822 Date format
14.Easter Date
15.Parse Date
16.compare Two dates with Day value
17.compare Two dates with Second value
18.Get start of a date
19.Get start date of a Month
20.Get start date of a year
21.Create date from year, month and day value
22.Create Date from year, month, day, hour, minute, second
23.Get year value from Date
24.Get Month value from Date
25.Get Day value from Date
26.Get now in Date and Millis-seconds
27.Add day, Month and year to a Date
28.Parse date in format of yyyyMMddHHmmss or yyyyMMdd
29.String to Date
30.Convert date value in long to YYYYMMDDHHMMSS format
31.Convert Date to Number
32.Get short and long date String
33.Convert Java Date To Xml Time
34.Convert dates to Julian dates.
35.Parse an RFC 822 date string.
36.Format a date into a format suitable for SQLite
37.Dot String to Date
38.Parses an RFC822 formatted date string.
39.Formats a Date according to RFC822.
40.DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(date);
41.yyyy-MM-dd HH:mm:ss date format
42.Utility class for formatting and parsing the various date formats we expect to encounter.
43.Get a string representation of a date as it relates to the current time.
44.get Date String from milliseconds
45.Generate a ISO 8601 date
46.Generate a Calendar from ISO 8601 date
47.parse Date for list of possible formats
48.date To String
49.Get date string for Locale tr
50.RFC3339 Date
51.Date formats
52.build Date Format day-of-week Short
53.Get end of each day
54.Get the end of each Month
55.Get end of a year
56.calculate Month Distance
57.calculate Day Distance
58.The month, and just the month, is zero-based. Add 1 for display.
59.Get hour different
60.format Millis Into Human Readable