31

Possible Duplicate:
converting long string to date

I need to convert long date value to mm/dd/yyyy format.my long value is

strDate1="1346524199000"

please help me

Community
  • 1
  • 1
andro-girl
  • 7,989
  • 22
  • 71
  • 94
  • 2
    Look here http://stackoverflow.com/questions/11753341/converting-long-string-to-date for a possible duplicate of your own question. – Andro Selva Aug 01 '12 at 08:44
  • http://stackoverflow.com/questions/11753341/converting-long-string-to-date this is long string to date convertion..i need long to date covertion.. – andro-girl Aug 01 '12 at 08:48
  • 1
    So where is the long datatype here. I could see only string data type. So it literally means that both the questions share the same idea. YOu have not shown any point which shows a long data type. – Andro Selva Aug 01 '12 at 08:53

4 Answers4

73

Refer Below code which give the date in String form.

import java.text.SimpleDateFormat;
import java.util.Date;



public class Test{

    public static void main(String[] args) {
        long val = 1346524199000l;
        Date date=new Date(val);
        SimpleDateFormat df2 = new SimpleDateFormat("dd/MM/yy");
        String dateText = df2.format(date);
        System.out.println(dateText);
    }
}
Suresh
  • 1,494
  • 3
  • 13
  • 14
  • 2
    Date date=new Date(1346524199000l); it will always give year 1970 – JosephM Mar 31 '17 at 12:30
  • 1
    Anyone who is having 1970 date from this code, please check your timestamp is in miliseconds. A timestamp for recent years should have 13 digits in it. If yours has 10 that means, you need to multiply by 1000. – Anuj Garg Feb 29 '20 at 18:37
8

Refer below code for formatting date

long strDate1 = 1346524199000;
Date date = new Date(strDate1);

try {
        SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
        SimpleDateFormat df2 = new SimpleDateFormat("dd/MM/yy");
        date = df2.format(format.parse("yourdate");
    } catch (java.text.ParseException e) {
        e.printStackTrace();
    }
Shiva kumar
  • 673
  • 8
  • 23
Nirali
  • 13,571
  • 6
  • 40
  • 53
2

Try this example

 String[] formats = new String[] {
   "yyyy-MM-dd",
   "yyyy-MM-dd HH:mm",
   "yyyy-MM-dd HH:mmZ",
   "yyyy-MM-dd HH:mm:ss.SSSZ",
   "yyyy-MM-dd'T'HH:mm:ss.SSSZ",
 };
 for (String format : formats) {
   SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.US);
   System.err.format("%30s %s\n", format, sdf.format(new Date(0)));
   sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
   System.err.format("%30s %s\n", format, sdf.format(new Date(0)));
 }

and read this http://developer.android.com/reference/java/text/SimpleDateFormat.html

om-nom-nom
  • 62,329
  • 13
  • 183
  • 228
Antaresm
  • 580
  • 2
  • 5
  • 19
1

Try something like this:

public class test 
{  

    public static void main(String a[])
    {  
        long tmp = 1346524199000;  

        Date d = new Date(tmp);  
        System.out.println(d);  
    }  
} 
Next Door Engineer
  • 2,818
  • 4
  • 20
  • 33