Java/Development Class/MessageFormat

Материал из Java эксперт
Версия от 07:00, 1 июня 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Combine date value in a sentence

import java.text.MessageFormat;
import java.util.Date;
public class Main {
  public static void main(String[] argv) throws Exception {
    Object[] params = new Object[] { new Date(), new Date(0) };
    String msg = MessageFormat.format("Today is {0,date} and UTC of 0 is {1,date}", params);
    System.out.println(msg);
  }
}





Currency number format

import java.text.MessageFormat;
public class Main {
  public static void main(String[] argv) throws Exception {
    Object[] params = new Object[] { new Double(123.45), new Double(1234.56) };
    String msg = MessageFormat.format("{0,number,currency} a""s and {1,number,currency} b""s",
        params);
    System.out.println(msg);
  }
}





Date value short format

import java.text.MessageFormat;
import java.util.Date;
public class Main {
  public static void main(String[] argv) throws Exception {
    Object[] params = new Object[] { new Date(), new Date(0) };
    String msg = MessageFormat.format("Today is {0,date,short} and UTC of 0 is {1,date,short}",
        params);
    System.out.println(msg);
  }
}





Floating point numbers

import java.text.MessageFormat;
public class Main {
  public static void main(String[] argv) throws Exception {
    Object[] params = new Object[] { new Double(123.45), new Double(1234.56) };
    String msg = MessageFormat.format("{0,number,#.#} a""s and {1,number,#.#} b""s", params);
    System.out.println(msg);
  }
}





Format Date() in long

import java.text.MessageFormat;
import java.util.Date;
public class Main {
  public static void main(String[] argv) throws Exception {
    Object[] params = new Object[] { new Date(), new Date(0) };
    String msg = MessageFormat.format("{0,time,long} and UTC of 0 is {1,time,long}", params);
    System.out.println(msg);
  }
}
//8:31:27 PDT AM and UTC of 0 is 4:00:00 PST PM





Format Date() in short format

import java.text.MessageFormat;
import java.util.Date;
public class Main {
  public static void main(String[] argv) throws Exception {
    Object[] params = new Object[] { new Date(), new Date(0) };
    String msg = MessageFormat.format("{0,time,short} and UTC of 0 is {1,time,short}", params);
    System.out.println(msg);
  }
}





Format Date value in full length

import java.text.MessageFormat;
import java.util.Date;
public class Main {
  public static void main(String[] argv) throws Exception {
    Object[] params = new Object[] { new Date(), new Date(0) };
    String  msg = MessageFormat.format("{0,time,full} and UTC of 0 is {1,time,full}", params);
    System.out.println(msg);
  }
}
//8:31:58 o"clock AM PDT and UTC of 0 is 4:00:00 o"clock PM PST





Format Date() value in medium format

import java.text.MessageFormat;
import java.util.Date;
public class Main {
  public static void main(String[] argv) throws Exception {
    Object[] params = new Object[] { new Date(), new Date(0) };
    String msg = MessageFormat.format("{0,time,medium} and UTC of 0 is {1,time,medium}", params);
    System.out.println(msg);
  }
}





Format message with Integer fillers

import java.text.MessageFormat;
public class Main {
  public static void main(String[] argv) throws Exception {
    Object[] params = new Object[] { new Integer(123), new Integer(1234) };
    String msg = MessageFormat.format("{0,number} a""s and {1,number} b""s", params);
    System.out.println(msg);
  }
}
//123 a"s and 1,234 b"s





Formatting a Message Containing a Date

import java.text.MessageFormat;
import java.util.Date;
public class Main {
  public static void main(String[] argv) throws Exception {
    Object[] params = new Object[] { new Date(), new Date(0) };
    String msg = MessageFormat.format("Today is {0} and UTC of 0 is {1}", params);
    System.out.println(msg);
  }
}





Formatting a Message Containing a Number

import java.text.MessageFormat;
public class Main {
  public static void main(String[] argv) throws Exception {
    Object[] params = new Object[] { new Integer(123), new Integer(1234) };
    String msg = MessageFormat.format("{0} a""s and {1} b""s", params);
    System.out.println(msg);
  }
}





Formatting a Message Containing a Time

import java.text.MessageFormat;
import java.util.Date;
public class Main {
  public static void main(String[] argv) throws Exception {
    Object[] params = new Object[] { new Date(), new Date(0) };
    String msg = MessageFormat.format("The time is {0} and UTC of 0 is {1}", params);
    msg = MessageFormat.format("The time is {0,time} and UTC of 0 is {1,time}", params);
    System.out.println(msg);
  }
}





Full length format for Date value

import java.text.MessageFormat;
import java.util.Date;
public class Main {
  public static void main(String[] argv) throws Exception {
    Object[] params = new Object[] { new Date(), new Date(0) };
    String msg = MessageFormat.format("Today is {0,date,full} and UTC of 0 is {1,date,full}",
        params);
    System.out.println(msg);
  }
}





Long style format for Date value

import java.text.MessageFormat;
import java.util.Date;
public class Main {
  public static void main(String[] argv) throws Exception {
    Object[] params = new Object[] { new Date(), new Date(0) };
    String msg = MessageFormat.format("Today is {0,date,long} and UTC of 0 is {1,date,long}",
        params);
    System.out.println(msg);
  }
}





Medium length format for Date value

import java.text.MessageFormat;
import java.util.Date;
public class Main {
  public static void main(String[] argv) throws Exception {
    Object[] params = new Object[] { new Date(), new Date(0) };
    String msg = MessageFormat.format("Today is {0,date,medium} and UTC of 0 is {1,date,medium}",
        params);
    System.out.println(msg);
  }
}
//





Percent value format

import java.text.MessageFormat;
public class Main {
  public static void main(String[] argv) throws Exception {
    Object[] params = new Object[] { new Double(123.45), new Double(1234.56) };
    String msg = MessageFormat
        .format("{0,number,percent} a""s and {1,number,percent} b""s", params);
    System.out.println(msg);
  }
}
//12,345% a"s and 123,456% b"s





Substitute tokens in a String

import java.text.MessageFormat;
public class Main {
  public static void main(String[] argv) {
    Object[] params = new Object[] { "hello", "!" };
    System.out.println(MessageFormat.format("{0} world {1}", params));
  }
}
//hello world !





Use a custom format

import java.text.MessageFormat;
public class Main {
  public static void main(String[] argv) throws Exception {
    Object[] params = new Object[] { new Integer(123), new Integer(1234) };
    String msg = MessageFormat.format("{0,number,#} a""s and {1,number,#} b""s", params);
    System.out.println(msg);
  }
}





Use a custom format for Date value

import java.text.MessageFormat;
import java.util.Date;
public class Main {
  public static void main(String[] argv) throws Exception {
    Object[] params = new Object[] { new Date(), new Date(0) };
    String msg = MessageFormat
        .format("{0,time,HH-mm-ss} and UTC of 0 is {1,time,HH-mm-ss}", params);
    System.out.println(msg);
  }
}
// 08-32-33 and UTC of 0 is 16-00-00