简体   繁体   中英

How can I convert a String from milliseconds to date?

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.

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

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

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

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

Code snippet to help:

String timestamp="20140809";
DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
System.out.println("date :"+dateFormat.parse(timestamp).getTime());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM