如何在 Android 中查找格式为 27-mar-2018 的日期
How to find date in a format like 27-mar-2018 in Android
我试图在 Android 应用程序中以 2018 年 3 月 27 日的格式从系统获取当前日期。我已经为此写了一些但它不起作用,因为它返回日期如“27-012-2018”。我也试过像 dd-mon-yyyy 但它抛出错误说不支持这种格式。我应该使用什么正确的模式来获取所需格式的当前日期。
提前致谢!
我的代码:
public class MainActivity extends AppCompatActivity {
RelativeLayout rldj;
RelativeLayout rlh;
RelativeLayout rlau;
RelativeLayout rldb;
RelativeLayout rlht;
RelativeLayout rlth;
RelativeLayout rltoi;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rldj = findViewById(R.id.rldj);
rlh = findViewById(R.id.rlh);
rlau = findViewById(R.id.rlau);
rldb = findViewById(R.id.rldb);
rlht = findViewById(R.id.rlht);
rlth = findViewById(R.id.rlth);
rltoi = findViewById(R.id.rltoi);
rlh.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, getDate(), Toast.LENGTH_SHORT).show();
}
});
}
private String getDate() {
DateFormat dateFormat = new SimpleDateFormat("dd-mmm-yyyy");
Date date = new Date();
return dateFormat.format(date);
}
}
而不是 dd-mmm-yyyy
使用 dd-MMM-yyyy
并将 dateFormat.format(date)
替换为 dateFormat.format(date).toUpperCase()
根据方法更改日期格式...
private String getDate() {
DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
Date date = new Date();
return dateFormat.format(date);
}
试试这个,它会提供你想要的东西
private String getDate() {
DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
Date date = new Date();
return dateFormat.format(date).toLowerCase();
}
使用 dd-MMM-2018 对我有用,但它仍然没有像我想要的那样返回日期,它像 2018 年 3 月 27 日那样返回,我希望它是 2018 年 3 月 27 日。
我已经根据您的建议进行了一些更改,现在我已经很完美了(2018 年 3 月 27 日)。
感谢大家的帮助。
这是更新后的代码:
private String getDate() {
DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
Date date = new Date();
return dateFormat.format(date).toLowerCase();
}
使用下面的方法
private String getDate()
{
android.text.format.DateFormat df = new android.text.format.DateFormat();
return (String) df.format("yyyy-MMM-dd", new java.util.Date());
}
对于日期转换,您可以使用以下代码:
public static String dateFormat(String dateStr) {
String inputFormat="dd-MM-yyyy"
String outputFormat="dd MMM yyyy"
String finalDate = "";
SimpleDateFormat inputSDF = new SimpleDateFormat(inputFormat);
SimpleDateFormat outputSDF = new SimpleDateFormat(outputFormat);
try {
Date date = inputSDF.parse(dateStr);
finalDate = outputSDF.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
Log.v("ffiinalDDateeEE", "finalDate: " + finalDate);
return finalDate;
}
我试图在 Android 应用程序中以 2018 年 3 月 27 日的格式从系统获取当前日期。我已经为此写了一些但它不起作用,因为它返回日期如“27-012-2018”。我也试过像 dd-mon-yyyy 但它抛出错误说不支持这种格式。我应该使用什么正确的模式来获取所需格式的当前日期。
提前致谢!
我的代码:
public class MainActivity extends AppCompatActivity {
RelativeLayout rldj;
RelativeLayout rlh;
RelativeLayout rlau;
RelativeLayout rldb;
RelativeLayout rlht;
RelativeLayout rlth;
RelativeLayout rltoi;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rldj = findViewById(R.id.rldj);
rlh = findViewById(R.id.rlh);
rlau = findViewById(R.id.rlau);
rldb = findViewById(R.id.rldb);
rlht = findViewById(R.id.rlht);
rlth = findViewById(R.id.rlth);
rltoi = findViewById(R.id.rltoi);
rlh.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, getDate(), Toast.LENGTH_SHORT).show();
}
});
}
private String getDate() {
DateFormat dateFormat = new SimpleDateFormat("dd-mmm-yyyy");
Date date = new Date();
return dateFormat.format(date);
}
}
而不是 dd-mmm-yyyy
使用 dd-MMM-yyyy
并将 dateFormat.format(date)
替换为 dateFormat.format(date).toUpperCase()
根据方法更改日期格式...
private String getDate() {
DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
Date date = new Date();
return dateFormat.format(date);
}
试试这个,它会提供你想要的东西
private String getDate() {
DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
Date date = new Date();
return dateFormat.format(date).toLowerCase();
}
使用 dd-MMM-2018 对我有用,但它仍然没有像我想要的那样返回日期,它像 2018 年 3 月 27 日那样返回,我希望它是 2018 年 3 月 27 日。 我已经根据您的建议进行了一些更改,现在我已经很完美了(2018 年 3 月 27 日)。 感谢大家的帮助。 这是更新后的代码:
private String getDate() {
DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
Date date = new Date();
return dateFormat.format(date).toLowerCase();
}
使用下面的方法
private String getDate()
{
android.text.format.DateFormat df = new android.text.format.DateFormat();
return (String) df.format("yyyy-MMM-dd", new java.util.Date());
}
对于日期转换,您可以使用以下代码:
public static String dateFormat(String dateStr) {
String inputFormat="dd-MM-yyyy"
String outputFormat="dd MMM yyyy"
String finalDate = "";
SimpleDateFormat inputSDF = new SimpleDateFormat(inputFormat);
SimpleDateFormat outputSDF = new SimpleDateFormat(outputFormat);
try {
Date date = inputSDF.parse(dateStr);
finalDate = outputSDF.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
Log.v("ffiinalDDateeEE", "finalDate: " + finalDate);
return finalDate;
}