java-Math、Random、Date、Calenda…

import java.util.*;
import java.text.*;
class DateDemo
{
<wbr>public static void main(String[] args)<br><wbr>{<br><wbr><wbr>//Date、Format、SimpleFormat.<br><wbr><wbr>Date d = new Date();<br><wbr><wbr>String str = "yyyy年MM月dd日 hh:mm:ss aa";<br><wbr><wbr>SimpleDateFormat sdf = new SimpleDateFormat(str);<br><wbr><wbr>String s = sdf.format(d);<br><wbr><wbr>System.out.println(s);<br><wbr><wbr><br><wbr><wbr>//Calender.<br><wbr><wbr>//Calendar c = Calendar.getInstance();<br><wbr><wbr>//System.out.println(c.get(Calendar.YEAR)+"年"+(c.get(Calendar.MONTH)+1)+"月"+c.get(Calendar.DAY_OF_MONTH )+"日");<br><wbr><wbr>getDay(2012);</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr>//Math.random() 随机数<br><wbr><wbr>getRandom();<br><wbr>}</wbr></wbr></wbr></wbr></wbr>

//某一年的二月有多少天.
<wbr>public static void getDay(int year)<br><wbr>{<br><wbr><wbr>Calendar c = Calendar.getInstance();<br><wbr><wbr>c.set(year, 2, 1); //月份从0开始计数<br><wbr><wbr>c.add(Calendar.DAY_OF_MONTH,-1);<br><wbr><wbr>//System.out.println(c.get(Calendar.YEAR)+"年"+(c.get(Calendar.MONTH)+1)+"月"+c.get(Calendar.DAY_OF_MONTH )+"日");<br><wbr><wbr>System.out.println(c.get(Calendar.DAY_OF_MONTH));<br><wbr>}</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr>//取10以内的10个随机数<br><wbr>public static void getRandom()<br><wbr>{<br><wbr><wbr>Random r = new Random();<br><wbr><wbr>for (int x = 0; x &lt; 10; x++)<br><wbr><wbr>{<br><wbr><wbr><wbr>//方法一:Math.random()<br><wbr><wbr><wbr>//double d = Math.random()*10+1;<br><wbr><wbr><wbr>//System.out.println((int)d);<br><wbr><wbr><wbr>//方法二:Random类<br><wbr><wbr><wbr><br><wbr><wbr><wbr>int a = r.nextInt(10);<br><wbr><wbr><wbr>System.out.println(a);<br><wbr><wbr>}<br><wbr>}<br> }</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr></wbr>

<wbr></wbr>

你可能感兴趣的:(Random)