0

可能重复:
如何在java中将当前日期转换为字符串?

如何以格式返回日期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);
        }
    }
}

我需要,例如,2012 年 2 月 11 日

4

2 回答 2

2

使用 SimpleDateFormat

SimpleDateFormat formatter= new SimpleDateFormat("dd/MM/yyyy");
System.out.println(formatter.format(new Date(randomTimestamp)));
于 2012-11-02T22:23:39.110 回答
1

SimpleDateFormat用于格式化和解析:

new SimpleDateFormat("dd/MM/yyyy").format(date);
于 2012-11-02T22:22:48.327 回答