Date and Time Format

2019-06-22 09:58发布

Greeting!

I've done a project that can send GPS Coordinates to mobile number Automatically, The Recipients received like this example format "lat:14.7836139 long:12.71104 speed:0.0 Date:1309325189000"
and now I want a format for a date and time like this Date:dd/mm/yy hh:mm Anyone who can Help me?

here is my sample code I use.

public void onLocationChanged(Location loc)

{

loc.getLatitude();

loc.getLongitude();
String Text = "lat:" + loc.getLatitude() + " "
+ "long:" + loc.getLongitude()+" "
+ "speed:" + loc.getSpeed() +" " 
+ "date:" + loc.getTime();   

Toast.makeText( getApplicationContext(),

Text,

Toast.LENGTH_SHORT).show();
String phoneNo = txtPhoneNo.getText().toString();


if (phoneNo.length()>0 && Text.length()>0)                
    sendSMS(phoneNo, Text);               


}

2条回答
【Aperson】
2楼-- · 2019-06-22 10:26

Being a development question, this should be moved to StackOverflow, but here's a tip: DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(loc.getTime()));

See the official documentation on the relevant classes around here to tweak it to suit your needs.

查看更多
The star\"
3楼-- · 2019-06-22 10:29

SimpleDateFormat should do the trick, you can then set the format mask yourself. Alternatively you could use the android Time class, but that is a bit more tricky mask wise.

SimpleDateFormat dateTimeFormat = new SimpleDateFormat("dd/MM/yy HH:mm");
dateTimeFormat.format(new Date(loc.getTime()));
查看更多
登录 后发表回答