0

Possible Duplicate:
how to convert current date into string in java?

How do I return the date in the format dd/mm/yyyy? in

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);
        }
    }
}

I need, example, 02/11/2012

Community
  • 1
  • 1
Regis Santos
  • 3,469
  • 8
  • 43
  • 65
  • See the Java Tutorial on this topic: http://docs.oracle.com/javase/tutorial/i18n/format/dateintro.html – DNA Nov 02 '12 at 22:35

2 Answers2

2

Use SimpleDateFormat

SimpleDateFormat formatter= new SimpleDateFormat("dd/MM/yyyy");
System.out.println(formatter.format(new Date(randomTimestamp)));
Chuidiang
  • 1,055
  • 6
  • 13
1

You use SimpleDateFormat for formatting as well as parsing:

new SimpleDateFormat("dd/MM/yyyy").format(date);
Keppil
  • 45,603
  • 8
  • 97
  • 119