Android设置的日期和时间不会显示在log cat中

时间:2017-07-26 06:58:50

标签: java android datetime

我的代码

用于在Android-Java中获取日期和时间

try {
            SimpleDateFormat sdf = new SimpleDateFormat(
                    "yyyy-MM-dd", Locale.US);
            String currentDateandTime = sdf.format(new Date());
            final File path = new File(Environment.getExternalStorageDirectory(), "SMOK_komunal");
            if (!path.exists()) {
                path.mkdir();
            }
            Log.e("path ", path.getAbsolutePath()+ " and "+currentDateandTime );

            Runtime.getRuntime().exec("logcat  -f " + path + File.separator + "dbo_logcat_" + currentDateandTime + ".txt");
        } catch (IOException e) {
            e.printStackTrace();
        }

记录值时。它不会显示日期和时间......任何人都可以帮助我吗?

3 个答案:

答案 0 :(得分:1)

 try {
                SimpleDateFormat sdf = new SimpleDateFormat(
                        "yyyy-MM-dd", Locale.US);
                String currentDateandTime = sdf.format(new Date());
                final File path = new File(Environment.getExternalStorageDirectory(), "SMOK_komunal");
                if (!path.exists()) {
                    path.mkdir();
                }
                Log.e("path ",""+path.getAbsolutePath());
                Log.e("Currentdateandtime",""+currentDateandTime );

                Runtime.getRuntime().exec("logcat  -f " + path + File.separator + "dbo_logcat_" + currentDateandTime + ".txt");
            } catch (IOException e) {
                e.printStackTrace();
            }

Android:获取日期和时间

public String timezone          =   TimeZone.getDefault().getID();

如果您使用Pure android,可以按照上面的步骤获取时区

如果您使用的是Java,则可以按照以下代码打印日期和时间 http://beginnersbook.com/2013/05/current-date-time-in-java/请参阅此

如果您使用的是android oncreate

使用我的代码..我得到了这个

的输出
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
//setContentView(R.layout.main); 

Calendar c = Calendar.getInstance(); 
System.out.println("Current time => "+c.getTime()); 

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
String formattedDate = df.format(c.getTime()); 
// formattedDate have current date/time 
Toast.makeText(this, formattedDate, Toast.LENGTH_SHORT).show(); 


// Now we display formattedDate value in TextView 
TextView txtView = new TextView(this); 
txtView.setText("Current Date and Time : "+formattedDate); 
txtView.setGravity(Gravity.CENTER); 
txtView.setTextSize(20); 
setContentView(txtView); 
}

答案 1 :(得分:0)

如果您的意思是时间没有在您的代码中退出,则表示您没有格式化时间。要显示时间,你必须像这样格式化。

           SimpleDateFormat sdf = new SimpleDateFormat(
                     "yyyy.MM.dd G 'at' HH:mm:ss z", Locale.US);
                String currentDateandTime = sdf.format(new Date());
                Log.e("TAG"," and "+currentDateandTime );

或者如果你的意思是你的日期没有退出。确保

  

new Date();

不返回null。 &安培;如果它不为null,请尝试注销

  

Log.e(“TAG”,“和”+ new Date());

并查看它是否显示空字符串。并相应地进行调试。

答案 2 :(得分:0)

您应该在执行logcat命令后记录,如下所示:

try {
        SimpleDateFormat sdf = new SimpleDateFormat(                    
                    "yyyy-MM-dd HH:mm:ss z", Locale.US);
        String currentDateandTime = sdf.format(new Date());
        final File path = new 
        File(Environment.getExternalStorageDirectory(), "SMOK_komunal");

        if (!path.exists()) {
            path.mkdir();
        }
        Runtime.getRuntime().exec("logcat -f " + path + File.separator 
        + "dbo_logcat_" + currentDateandTime + ".txt" + " *:E");

        Log.e("path ",""+path.getAbsolutePath());
        Log.e("Currentdateandtime",""+currentDateandTime );


    } catch (IOException e) {
        e.printStackTrace();