如何以dd/mm/yyyy
格式返回日期?在
package random01;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
public class Random01 {
public static void main(String[] args) throws ParseException {
Random random = new Random();
String[] v = new String[5];
for (int i = 0;
i < v.length;
i++) {
//long tempo = (long) (1293861598 + random.nextDouble() * 60 * 60 * 24 * 365);
DateFormat dateFormat = new SimpleDateFormat("yyyy");
Date dateFrom = dateFormat.parse("2012");
long timestampFrom = dateFrom.getTime();
Date dateTo = dateFormat.parse("2013");
long timestampTo = dateTo.getTime();
long timeRange = timestampTo - timestampFrom;
long randomTimestamp = timestampFrom + (long) (random.nextDouble() * timeRange);
System.out.println(randomTimestamp);
}
}
}
我需要,例如,02/11/2012
答案 0 :(得分:2)
使用SimpleDateFormat
SimpleDateFormat formatter= new SimpleDateFormat("dd/MM/yyyy");
System.out.println(formatter.format(new Date(randomTimestamp)));
答案 1 :(得分:1)
您使用SimpleDateFormat
进行格式化和解析:
new SimpleDateFormat("dd/MM/yyyy").format(date);