0

I have this code:

String id = c.getString("data"); 
String name = ((TextView) view.findViewById(R.id.TextView05)).getText().toString();

public static String getDate(long seconds, String dateFormat)
{
    DateFormat formatter = new SimpleDateFormat("yyyy MMMM dd HH:mm");
    long now = System.currentTimeMillis();
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(now);
    return formatter.format(calendar.getTime());
}

The "data" is 1341435600000. I want to have this String from milliseconds to date.

hata
  • 11,633
  • 6
  • 46
  • 69

6 Answers6

0
try {  
      String str_date="11-June-07";
      DateFormat formatter ; 
      Date date ; 
      formatter = new SimpleDateFormat("dd-MMM-yy");
      date = (Date)formatter.parse(str_date);  
      System.out.println("Today is " +date );
    } 
catch (ParseException e)
{
System.out.println("Exception :"+e);
}

for convert milliseconds to Date

long yourmilliseconds = 1119193190;
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd,yyyy HH:mm");
Date resultdate = new Date(yourmilliseconds);
mob_web_dev
  • 2,342
  • 1
  • 20
  • 42
0

Something like this:

    SimpleDateFormat ss = (SimpleDateFormat) SimpleDateFormat.getInstance();
    ss.applyPattern("yyyy MM dd HH:mm");

    // Date to String: 
    String dateToString = ss.format(new Date());

    //String to Date
    Date stringToDate = ss.parse("2012 02 16 14:10"));
Vyacheslav Shylkin
  • 9,741
  • 5
  • 39
  • 34
0
    String dtStart = "2010-10-15T09:27:37Z";  
SimpleDateFormat  format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");  
try {  
    Date date = format.parse(dtStart);  
    System.out.println(date);  
} catch (ParseException e) {  
    // TODO Auto-generated catch block  
    e.printStackTrace();  
}
iAndroid
  • 951
  • 7
  • 18
0

The same method is working fine for me. Why don't yours?

/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    long cTime = System.currentTimeMillis();

    String date = getDate(cTime, "dd/MM/yyyy hh:mm:ss.SSS");

    Toast.makeText(getApplicationContext(), date, Toast.LENGTH_SHORT).show();
}

public static String getDate(long milliSeconds, String dateFormat)
{
    // Create a DateFormatter object for displaying date in specified format.
    DateFormat formatter = new SimpleDateFormat(dateFormat);

    // Create a calendar object that will convert the date and time value in milliseconds to date. 
     Calendar calendar = Calendar.getInstance();
     calendar.setTimeInMillis(milliSeconds);
     return formatter.format(calendar.getTime());
}
Praveenkumar
  • 24,084
  • 23
  • 95
  • 173
  • String id = Utils.getDate(c.getString("data"), "dd/MM/yyyy"); will be right? – user1563977 Jul 31 '12 at 11:11
  • @user1563977 If your `data` contains long value of time's milliseconds. That would be right. Just do that like - `long cTime = c.getString("data");` Put this code instead of `cTime` in above answer – Praveenkumar Jul 31 '12 at 11:18
  • `String id = c.getString(TAG_DATA); long milliseconds = Long.parseLong(id); String date = Utils.getDate(milliseconds, "yyyy-MM-dd HH:mm:ss");` Solved :) Another question i have :) what should i write to know what GMT is ? – user1563977 Jul 31 '12 at 17:34
0

if you are not bothered about the Date format, just use it as below

String id = c.getString("data"); //"1341435600000"; 
Date date123 = new Date(Long.parseLong(id));
System.out.println(date123);
sunil
  • 6,444
  • 1
  • 32
  • 44
0

Code snippet to help:

String timestamp="20140809";
DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
System.out.println("date :"+dateFormat.parse(timestamp).getTime());
sa_nyc
  • 971
  • 1
  • 13
  • 23