次のコードを書きました
Date d = new Date();
CharSequence s = DateFormat.format("MMMM d, yyyy ", d.getTime());
しかし、私にパラメーターを求めています。現在の日付を文字列形式で表示したいのですが、
お気に入り
28-Dec-2011
設定できるようにTextView
、
少し説明してください。何か必要だと思われる場合は、私は Android 開発の初心者です。
次のコードを書きました
Date d = new Date();
CharSequence s = DateFormat.format("MMMM d, yyyy ", d.getTime());
しかし、私にパラメーターを求めています。現在の日付を文字列形式で表示したいのですが、
お気に入り
28-Dec-2011
設定できるようにTextView
、
少し説明してください。何か必要だと思われる場合は、私は Android 開発の初心者です。
このクラスを使用して、SimpleDateFormat
日付を希望の形式にフォーマットできます。
例のアイデアが得られるこのリンクを確認してください。
例えば:
String dateStr = "04/05/2010";
SimpleDateFormat curFormater = new SimpleDateFormat("dd/MM/yyyy");
Date dateObj = curFormater.parse(dateStr);
SimpleDateFormat postFormater = new SimpleDateFormat("MMMM dd, yyyy");
String newDateStr = postFormater.format(dateObj);
詳細な例はここにあります。この例を実行して、SimpleDateFormatクラスの概念を理解することをお勧めします。
Date c = Calendar.getInstance().getTime();
System.out.println("Current time => " + c);
SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy", Locale.getDefault());
String formattedDate = df.format(c);
このyyyy-MM-dd形式で現在の日付を取得するための単純な1 行のコードで、必要な形式を使用できます。
String date = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(new Date());
Android は Java ベースであるため、Android とは関係ありません。
private String getDateTime() {
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
return dateFormat.format(date);
}
public String giveDate() {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("EEE, MMM d, yyyy");
return sdf.format(cal.getTime());
}
これを試して、
SimpleDateFormat timeStampFormat = new SimpleDateFormat("yyyyMMddHHmmssSS");
Date myDate = new Date();
String filename = timeStampFormat.format(myDate);
CharSequence s = DateFormat.getDateInstance().format("MMMM d, yyyy ");
最初にインスタンスが必要です
チャームのように機能し、ボーナスとして String に変換します ;)
SimpleDateFormat currentDate = new SimpleDateFormat("dd/MM/yyyy");
Date todayDate = new Date();
String thisDate = currentDate.format(todayDate);
String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
// Date クラスをjava.utilとしてインポート
以下のコードは、時刻と日付の両方を表示します
Calendar cal = Calendar.getInstance();
cal.getTime().toString();
public static String getcurrentDateAndTime(){
Date c = Calendar.getInstance().getTime();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
String formattedDate = simpleDateFormat.format(c);
return formattedDate;
}
// String currentdate= getcurrentDateAndTime();
Calendar cal = Calendar.getInstance();
Calendar dt = Calendar.getInstance();
dt.clear();
dt.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),cal.get(Calendar.DATE));
return dt.getTime();
CalendarView を使用してカレンダー アプリを作成しました。これが私のコードです。
CalendarView cal = (CalendarView) findViewById(R.id.calendar);
cal.setDate(new Date().getTime());
「カレンダー」フィールドは私の CalendarView です。輸入品:
import android.widget.CalendarView;
import java.util.Date;
エラーなしで現在の日付を取得しました。
public static String getDateTime() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMMM dd, yyyy HH:mm:ss", Locale.getDefault());
Date date = new Date();
return simpleDateFormat.format(date);
}
現在のロケール (デバイス ロケール!) で現在の日付を取得する最も簡単な方法:
String currentDate = DateFormat.getDateInstance().format(Calendar.getInstance().getTime());
異なるスタイルで日付を表示したい場合は、次を使用しますgetDateInstance(int style)
。
DateFormat.getDateInstance(DateFormat.FULL).format(Calendar.getInstance().getTime());
その他のスタイル: DateFormat.LONG
、DateFormat.DATE_FIELD
、DateFormat.DAY_OF_YEAR_FIELD
など (CTRL+Spaceすべてを表示するには を使用)
時間が必要な場合:
String currentDateTime = DateFormat.getDateTimeInstance(DateFormat.DEFAULT,DateFormat.LONG).format(Calendar.getInstance().getTime());
このアプローチで試してみましたが、うまくいきました。
val df = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.getDefault()) // pass the format pattern that you like and done.
println(df.format(Date()))
コトリンで
https://www.programiz.com/kotlin-programming/examples/current-date-time
fun main(args: Array<String>) {
val current = LocalDateTime.now()
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS")
val formatted = current.format(formatter)
println("Current Date and Time is: $formatted")}
このコードのリンクを試してみてください。これは、日付と時刻のすべてのケースに対する絶対的な正解です。または、アプリの必要性と要件に従って日付と時刻をカスタマイズします。
このリンクで試してください。このリンクで試してください
これは私が使用したコードです:
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
// Set current date into textview
tvDisplayDate.setText(new StringBuilder()
.append(month + 1).append("-") // Month is 0 based, add 1
.append(day).append("-")
.append(year).append(" Today is :" + thursday ) );
// Set current date into datepicker
dpResult.init(year, month, day, null);